/**
This program computes the total number of Powerball lottery combinations:
- Choose 5 distinct numbers out of 69
- Choose 1 Powerball number out of 26
Total combinations = C(69, 5) * 26
It uses:
- An efficient integer-based binomial coefficient function
- long for large values
- A helper function to format numbers with commas
*/
public class PowerballCombinations {
/**
binomial(n, k):
Computes C(n, k) using the multiplicative formula:
C(n, k) = Π (n - i + 1) / i for i = 1..k
This avoids:
- factorial overflow
- floating point inaccuracies
*/
public static long binomial(int n, int k) {
if (k > n) return 0;
if (k > n - k) k = n - k; // symmetry: C(n, k) = C(n, n-k)
long result = 1;
for (int i = 1; i <= k; i++) {
result = result * (n - i + 1) / i;
}
return result;
}
/**
formatWithCommas(value):
Converts a long into a comma-formatted string.
Example:
11238513 -> "11,238,513"
*/
public static String formatWithCommas(long value) {
return String.format("%,d", value);
}
public static void main(String[] args) {
final int MAIN_COUNT = 5;
final int MAIN_MAX = 69;
final int POWER_MAX = 26;
// Compute C(69, 5)
long mainCombinations = binomial(MAIN_MAX, MAIN_COUNT);
// Multiply by 26 Powerball choices
long totalCombinations = mainCombinations * POWER_MAX;
System.out.println("Powerball combinations (5 out of 69 and 1 out of 26):");
System.out.println("C(69, 5) = " + formatWithCommas(mainCombinations));
System.out.println("Total combinations = " + formatWithCommas(totalCombinations));
}
}
/*
run:
Powerball combinations (5 out of 69 and 1 out of 26):
C(69, 5) = 11,238,513
Total combinations = 292,201,338
*/