python maximum recursion depth exceeded解决方式

用Python写了一个递归脚本,脚本如下

 

def fact(n):
    return fact_iter(n, 1)


def fact_iter(num, product):
    if num == 1:
        return product
    return fact_iter(num - 1, num*product)


执行:fact(1000)

 

报错如下:

  File "D:/python/spider/qq-music-spider/tt.py", line 12, in fact_iter
    return fact_iter(num - 1, num*product)
  File "D:/python/spider/qq-music-spider/tt.py", line 12, in fact_iter
    return fact_iter(num - 1, num*product)
  File "D:/python/spider/qq-music-spider/tt.py", line 12, in fact_iter
    return fact_iter(num - 1, num*product)
  File "D:/python/spider/qq-music-spider/tt.py", line 12, in fact_iter
    return fact_iter(num - 1, num*product)
  File "D:/python/spider/qq-music-spider/tt.py", line 12, in fact_iter
    return fact_iter(num - 1, num*product)
  File "D:/python/spider/qq-music-spider/tt.py", line 12, in fact_iter
    return fact_iter(num - 1, num*product)
  File "D:/python/spider/qq-music-spider/tt.py", line 10, in fact_iter
    if num == 1:
RecursionError: maximum recursion depth exceeded in comparison

报错提示超过最大递归深度。

 

该问题解决方式为加入如下脚本:

import sys

sys.setrecursionlimit(1000000) #例如这里设置为一百万

执行:fact(1000),能正确出结果了。

 

 

 

但是,好像只可以执行到 fact(3930) 左右。

执行:fact(3930),报错如下:

Process finished with exit code -1073741571 (0xC00000FD)

该问题可参考这篇博文,此处不再做具体讨论:

 

https://blog.csdn.net/vblittleboy/article/details/6613815

 

你可能感兴趣的:(python,python,递归)