Created
December 18, 2014 16:06
-
-
Save soltrinox/e51ea35c2a3d64d20cc6 to your computer and use it in GitHub Desktop.
Factorial/Binomial Coefficent (N Choose K) nCk=n!/(k!*(n-k)!)
This file contains hidden or 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
| public static long factorial(int a){ //non-recursive factorial, returns long | |
| int answer=1; | |
| for(int i=1;i<=a;i++){ //starts from 1, multiplies up, such as 1*2*3... | |
| answer*=i; | |
| } | |
| return(answer); | |
| } | |
| public static long recFactorial(int a){ //recurive factorial, returns long | |
| if(a==1){ //stopping case for recursion: if a=1; 1!=1. | |
| return(1); | |
| } | |
| return(a*recFactorial(a-1)); //recursion: a!=a*(a-1)! | |
| } | |
| public static long comboChoose(int n, int k){ //combinatorics function. takes n,k | |
| return(factorial(n)/(factorial(k)*factorial(n-k))); //definition of nCk. | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment