递归限制在阶乘函数中给出错误 [英] Recursion limit gives error in factorial function
本文介绍了递归限制在阶乘函数中给出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
导入系统num = long(raw_input("请输入阶乘的数字 -> "))sys.setrecursionlimit(num + 1)事实上(n):如果 n == 0 或 n == 1:返回 1别的:返回(n * 事实(n - 1))打印事实(长(数))
以上是我用于查找阶乘的代码,由于最大递归限制深度为997",因此我尝试使用 :-
<块引用>导入系统
sys.setrecursionlimit()
但是还是报错.我还能做什么?
解决方案
import sysnum = long(raw_input("请输入阶乘的数字 -> "))sys.setrecursionlimit(num + 1)事实上(n):如果 n == 0 或 n == 1:返回 1别的:返回(n * 事实(n - 1))打印打印因子是 -> ",事实(int(num))
<块引用>
这很完美,我们可以增加限制> 997
import sys
num = long(raw_input("Enter the number for the factorial -> "))
sys.setrecursionlimit(num + 1)
def fact(n):
if n == 0 or n == 1:
return 1
else:
return (n * fact(n - 1))
print fact(long(num))
Above is the code I used for finding factorial and since the maximum recursion limit depth is '997', I tried to change that using :-
import sys
sys.setrecursionlimit()
But it still gives error. What else can I do ?
解决方案
import sys
num = long(raw_input("Enter the number for the factorial -> "))
sys.setrecursionlimit(num + 1)
def fact(n):
if n == 0 or n == 1:
return 1
else:
return (n * fact(n - 1))
print
print "Factorial is -> ", fact(int(num))
This works perfectly and we can increase the limit > 997
这篇关于递归限制在阶乘函数中给出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文