【发布时间】:2013-11-30 18:58:45
【问题描述】:
我写了这段代码来计算组合的数量:
def fact(n):
return 1 if(n == 1) else n * fact(n - 1)
def combinations(n,k):
return fact(n)/((fact(n - k) * fact(k)))
while(True):
print(combinations(int(input()), int(input())))
阶乘函数似乎工作正常。但是,当我尝试查找两个数字的组合时,为什么它会给我一个超出比较错误的最大递归深度呢?阶乘函数是否有问题,因为这似乎是错误的来源?
这是我得到的错误:
builtins.RuntimeError:比较中超出了最大递归深度
【问题讨论】:
-
math.factorial有什么问题吗? -
scipy.misc.comb有什么问题?
标签: python recursion combinations factorial