Skip to content

Instantly share code, notes, and snippets.

@david-bergstrom
Last active August 29, 2015 14:10
Show Gist options
  • Save david-bergstrom/a2cc5170a1b3665bc671 to your computer and use it in GitHub Desktop.
Save david-bergstrom/a2cc5170a1b3665bc671 to your computer and use it in GitHub Desktop.
A simple algorithm for calculating the binomial coefficient written i C++.
#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