I found on Wikipedia that C(n,k) = (n-k+1)/k * C(n, k-1). I did the evidence myself and got that it was correct. Then I implemented it in my function, which is suppose to calculate combinations using recursion without hitting the built-in limit in python. This worked great.. until I put in to great numbers. This is my function:
def choose(n, k):
if not k:
return 1
elif n < k:
return 0
else:
return int((((n - k + 1) / k)) * choose(n, (k - 1)))
It worked if you would use lower numbers such as choose(1000, 4), but if you try numbers such as choose(1000,800) it will return the first 13 number right, but then get everything wrong. How come this occurs and even more important, how do you solve it?