Skip to content

Instantly share code, notes, and snippets.

@hertz1
Last active September 22, 2015 00:53
Show Gist options
  • Save hertz1/ba0baa8409f5b6d99707 to your computer and use it in GitHub Desktop.
Save hertz1/ba0baa8409f5b6d99707 to your computer and use it in GitHub Desktop.
Binomial function. Calculates the number of ways of picking k unordered from the given number of possibilities.
/**
* Calculates the number of ways of picking k unordered.
* @params {Interger}
* @return {Interger}
*/
function binomial(n, k) {
if (k === 0) {
return 1;
} else if (2*k > n) {
return binomial(n, n-k);
} else {
e = n-k + 1;
for (i = 2; i < k +1; i++) {
e *= (n-k + i);
e /= i;
}
return e;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment