我有下面的代码,这似乎是为了计算阶乘。
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
n=int(input("Input a number to compute the factiorial : "))
print(factorial(n))例如,阶乘5= 120。这可以使用下面的代码来实现。
但是,当我使用Python IDLE输入"1003“作为输入时,输出是6。
有人能解释这个吗?下面是截图。
重要提示:只有在我运行了几个测试后才会发生这种情况:
例如,尝试999,1000,然后是9000,然后是1003。在最后一次测试中,6作为输出出现。在IDLE中发生了什么导致这种情况?

发布于 2021-08-14 14:58:14
也许JS解决方案可以帮助您:
function factorial (y){
if (y ==0 || y ==1){
return 1;
}
else {
f = y - 1;
while (f >= 1) {
y = y * f;
f--;
}
return y;
}
}
console.log(factorial(100));https://stackoverflow.com/questions/68439915
复制相似问题