python中怎么有效快速地计算二项式系数?
2个回答
可以直接使用scipy
>>>from scipy.special import comb
>>>comb(10, 3, exact=True) #精确解
120
>>>comb(10, 3, exact=False) #近似解
120.0自己写一个也不费事
def binom_coef(n, k):
    result = 1
    for i in range(n-k+1, n+1):
        result *= i
    for i in range(1, k+1):
        result /= i
    return result例子
>>>binom_coef(5, 2)
10.0
>>>binom_coef(6, 1)
6.0
    
  相关讨论
  随便看看