我需要一個函數可以解決以下問題:對於二項函數nCr = k,給定r和k找到n。在數學nCr = n!/ r!(n-r)!我嘗試了以下,但它並沒有解決它。例如8C6 = 28,對於我的函數輸入是6和28,我想找到8.這可能沒有確切的整數,所以我想找到一個x> = n。解決二項函數的Python函數
"""
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 /(nr)!在「sum」中並將其與r!k進行比較,但「sum」計算錯誤。 (n-1)^ 2,則(n-2)^ 2 *(n-1)等等... – jomuel
可能的重複[對於二項函數nCr = k,給定r和k找到n](http://stackoverflow.com/questions/35753215/for-binomial-function-ncr-k-given-r-and-k-find-n) –
嗨@jomuel我修改它和但有時候不會得到結果。 –