Last active
September 22, 2015 00:53
-
-
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.
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
/** | |
* 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