Skip to content

Instantly share code, notes, and snippets.

@soltrinox
Created December 18, 2014 16:06
Show Gist options
  • Select an option

  • Save soltrinox/e51ea35c2a3d64d20cc6 to your computer and use it in GitHub Desktop.

Select an option

Save soltrinox/e51ea35c2a3d64d20cc6 to your computer and use it in GitHub Desktop.
Factorial/Binomial Coefficent (N Choose K) nCk=n!/(k!*(n-k)!)
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