Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion for answer to question 23 #4

Open
dougmvieira opened this issue Feb 18, 2020 · 1 comment
Open

Suggestion for answer to question 23 #4

dougmvieira opened this issue Feb 18, 2020 · 1 comment

Comments

@dougmvieira
Copy link

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/ .

@dwcoder
Copy link
Owner

dwcoder commented Mar 10, 2020

This is very helpful, thanks. I will see whether I can work it into the next version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants