2 years ago

#70669

test-img

Alexius

Why is this second recursive factorial function wrong

So the first solution is the correct one, but the second one is giving me the same answer as the first one in every case I have tried. It felt to me like the second solution should work intuitively but it also seems wrong. Can someone explain why it is wrong?

def factorial(n):
  if n in [0, 1]:
    return 1
  else:
    return n * factorial(n-1)

print(factorial(4))

def factorial2(n):
  while n >= 0:
    return n * factorial(n-1)

print(factorial2(4))

recursion

factorial

0 Answers

Your Answer

Accepted video resources