3

I have a (very) simple recursive function:

def factorial(n):
    if n == 1:
        return 1
    return n * factorial(n-1)

The maximum recursion depth is 3000, found by running:

import sys
sys.getrecursionlimit()

But factorial(2986) is the largest number that successfully runs. What accounts for the difference?

David Stevens
  • 835
  • 1
  • 6
  • 15
  • 1
    Just a guess: the very file in which the `factorial` function is found may already be running at a non-zero stack depth. Your python environment may create some amount of stack in the process of running the file. – Gershom Maes Mar 27 '19 at 21:17
  • I guess you can change the recursion limit programmatically by calling `sys.setrecursionlimit(4000)`, although I don't think that's a good idea. – dcg Mar 27 '19 at 21:21

0 Answers0