QuantitativePrimer
QuantitativePrimer copied to clipboard
Suggestion for answer to question 23
Thanks a lot for the primer! When reading question 23, I thought the "recursion" part was meant on the binomial function itself instead of the factorial, so I came up with:
def n_given_k(n, k):
if (k == 0) or (k == n):
return 1
return n_given_k(n - 1, k - 1) + n_given_k(n - 1, k)
For reference: https://en.wikipedia.org/wiki/Binomial_coefficient#Recursive_formula
It answers the question, although it is of course very inefficient. A better recursive formula is given in http://penguin.ewu.edu/~trolfe/BinomialCoeff/ .
This is very helpful, thanks. I will see whether I can work it into the next version.