【发布时间】:2016-05-10 19:54:18
【问题描述】:
我需要一个可以解决以下问题的函数:对于二项式函数 nCr=k,给定 r 和 k 找到 n。在数学中 nCr=n!/r!(n-r)!我尝试了以下但它没有解决它。例如 8C6=28,对于我的函数,输入是 6 和 28,我想找到 8。这可能没有确切的整数,所以我想找到一个 x>=n。
"""
I am approaching it this way, i.e. find the solution of a polynomial function iteratively, hope there is a better way"""
"""I am approaching it this way, i.e. find the solution of a polynomial function iteratively, hope there is a better way"""
def find_n(r,k):
#solve_for_n_in(n*(n-1)...(n-r)=math.factorial(r)*k
#in the above example solve_for_n(n*(n-1)(n-2)(n-3)(n-4)(n-5)=720*28)
sum=math.factorial(r)*k
n=r
p=1
while p<sum:
p=1
for i in range(0,r+2):
p*=(n-i+1)
n+=1
return n
谢谢。
【问题讨论】:
-
只要正确计算......你的想法显然是存储 n/(n-r)!在“sum”中并将其与 r!k 进行比较,但“sum”计算错误。在第一次迭代中,总和等于 n,然后是 (n-1)^2,然后是 (n-2)^2*(n-1),依此类推...
-
嗨@jomuel 我修改了它,但有时它没有得到结果。
标签: python numpy combinatorics itertools discrete-mathematics