Last active
August 29, 2015 14:10
-
-
Save david-bergstrom/a2cc5170a1b3665bc671 to your computer and use it in GitHub Desktop.
A simple algorithm for calculating the binomial coefficient written i C++.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
int choose(int n, int k) | |
{ | |
if(k > n) | |
return -1; | |
else if(k > n - k) | |
return choose(n, n - k); | |
else if(k == 1) | |
return n; | |
else | |
return (choose(n, k - 1) * (n - k + 1)) / k; | |
} | |
int main() | |
{ | |
std::cout << choose(500, 4) << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment