1

I'm trying to write a code to find the solution for a,b and c. What am I missing? If you don't want to use math library and define factorial and then find the solution.

 num = input("Enter a number: ")
def fact(n):
 if n == 1:
  return n
 elif n < 1:
  return ("NA")
 else:
  return n*fact(n-1)
print (fact(int(num)))


a=1
b=1
c=0
while a<100:
  while b<100:
    while c<100:
     #fact(a)*fact(b)=fact(a)+fact(b)+fact(c)
     if (fact(a)*fact(b))==(fact(a)+fact(b)+fact(c)):
       print(int(a),int(b),int(c))
       break
     else:
      c=c+1
    return
  • Your loop never increments `a` or `b`. Why don't you use `for a in range (1, 100): for b in range(1, 100): for c in range(1, 100):`? – Barmar Dec 11 '20 at 22:34
  • You can't use `c=0` since `fact(0)` doesn't exist. – Barmar Dec 11 '20 at 22:35
  • `break` only breaks out of the inner loop. See https://stackoverflow.com/questions/189645/how-to-break-out-of-multiple-loops for how to break out of the outer loops. – Barmar Dec 11 '20 at 22:36
  • You should implement memoization in your `fact()` function, otherwise this will be really slow. – Barmar Dec 11 '20 at 22:37
  • What does `print(fact(int(num)))` have to do with your problem. – Barmar Dec 11 '20 at 22:38
  • 1
    @Barmar I think c = 0 is OK, isn't it? since 0! = 1. – Robert Dodier Dec 12 '20 at 01:20
  • 2
    You've made a good start. Some advice about debugging. (1) Make the problem smaller. Try searching < 10 instead of < 100, and then increase the limit when you get it working. (2) Print out the result for every combination, even the ones which don't pass the test. Verify that for every combination, passed or failed, you can see that the result is correct. Good luck, and keep up the good work. – Robert Dodier Dec 12 '20 at 01:24
  • @RobertDodier His `fact()` function returns `'NA'` when `n < 1` – Barmar Dec 12 '20 at 01:56
  • 2
    This is a math problem at heart, not a programming one. Show that any solution must have `a = b`, and see where that takes you. – dxiv Dec 12 '20 at 06:29
  • Here is [a video](https://www.youtube.com/watch?v=9dyK_op-Ocw) about the equation. – JohanC Dec 12 '20 at 19:32

0 Answers0