Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.math.BigInteger;
- import java.util.Scanner;
- /**
- * Nate Wheeler
- * compsci 220
- * Hw 11
- * nrwheeler@student.stcc.edu
- *
- */
- public class Combinations {
- public static BigInteger getNumberOfCombinations(int n, int k) {
- BigInteger nfact = BigInteger.ONE;
- BigInteger kfact = BigInteger.ONE;
- int h = n - k;
- BigInteger hfact = BigInteger.ONE;
- // factorial of n
- for (int i = 1; i <= n; i++)
- nfact = nfact.multiply(BigInteger.valueOf((long) i));
- // factorial of k
- for (int i = 1; i <= k; i++)
- kfact = kfact.multiply(BigInteger.valueOf((long) i));
- // factorial of the difference between n and k
- for (int i = 1; i <= h; i++)
- hfact = hfact.multiply(BigInteger.valueOf((long) i));
- // basic arithmetic
- BigInteger combin = kfact.multiply(hfact);
- combin = nfact.divide(combin);
- // number of combinations
- return combin;
- }
- public static void main(String[] args) {
- System.out.println("Enter n: ");
- Scanner ncom = new Scanner(System.in);
- System.out.println("Enter k: ");
- Scanner kcom = new Scanner(System.in);
- int n = ncom.nextInt();
- int k = kcom.nextInt();
- System.out.println("The C(n, k) is " + getNumberOfCombinations(n, k));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement