Home » Python » The maximum depth of Python and the recursive fact...

The maximum depth of Python and the recursive factorial

factorial two implementation methods


# uses function recursion, 
def factorial_v1 (n) :
if n = = 0:
return 1
else:
return n * factorial_v1 (n-1)
# uses the for loop,
def factorial_v2 (n) :
Result = 1
for, I, in, range (n+1):
Result = result * I = I = = 0 else result if
return result
if __name__ = = '__main__':
print factorial_v1 (20)
print factorial_v2 (20)

function recursive maximum depth


def factorial (n) : 
if n = = 0:
return 1
else:
return n * factorial_v1 (n-1)
if __name__ = = '__main__':
print factorial (1000)

When

calls the factorial_v1 (1000) function to 1000, the following error is reported:

RuntimeError:, maximum, recursion, depth, exceeded


The

indicates that the maximum depth is

by using the method that exceeds the maximum call depth
import sys
sys.setrecursionlimit (10000)
# sets the maximum depth of 10 thousand

Latest