$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · medium · #36

What is the correct fix?

def factorial(n):
    if n == 0:
        return 1
    return n * factorial(n)
  1. Change factorial(n) to factorial(n - 1)
  2. Change the base case to n == 1
  3. Change * to + in the recursion
  4. Remove the base case from the function
The recursive call must shrink n, otherwise it never ends.
Play Problem Solving ▸