import java.util.Scanner;
Demonstration of a solution to the N choose K problem (binomial coefficients).
<p>
<= k <= n - 1
( k ) ( k-1 ) ( k )
( n ) ( n )
( ) = ( ) = 1 for n >
</p>
@author
@version
public class NChooseK{
Recursive solution to N choose K
@param n
@param k
@return
public static int nChooseK( int n, int k ){
int result = 0;
System.out.println( "nChooseK( " + n + ", " + k + ") returning "+ result);
return result;
}
public static void main( String[] args ){
Scanner stdinScanner = new Scanner( System.in );
int n = 0;
int k = 0;
System.out.print( "Please enter n: " );
n = stdinScanner.nextInt();
System.out.print( "Please enter k: " );
k = stdinScanner.nextInt();
if( k < 0 || k > n || n < 0 ){
System.out.println("Invalid arguments: n: " + n + ", k: " + k);
return;
}
System.out.println( n + " choose " + k + " = " + nChooseK( n, k ));
}
}