import Foundation
/*
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:
- UInt64 for safe integer arithmetic (values fit comfortably)
- an efficient binomial coefficient function
- a helper to format numbers with commas
*/
/*
binomial(n, k):
Computes C(n, k) using the multiplicative formula:
C(n, k) = Π (n - i + 1) / i for i = 1..k
Implemented using UInt64 because the results are small enough
(Powerball values never exceed ~300 million).
*/
func binomial(_ n: Int, _ k: Int) -> UInt64 {
if k > n { return 0 }
let kk = min(k, n - k)
var result: UInt64 = 1
for i in 1...kk {
result = result * UInt64(n - i + 1) / UInt64(i)
}
return result
}
/*
formatWithCommas(value):
Formats a UInt64 with commas.
*/
func formatWithCommas(_ value: UInt64) -> String {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
return formatter.string(from: NSNumber(value: value)) ?? "\(value)"
}
let MAIN_COUNT = 5
let MAIN_MAX = 69
let POWER_MAX = 26
// Compute C(69, 5)
let mainCombinations = binomial(MAIN_MAX, MAIN_COUNT)
// Multiply by 26 Powerball choices
let totalCombinations = mainCombinations * UInt64(POWER_MAX)
print("Powerball combinations (5 out of 69 and 1 out of 26):")
print("C(69, 5) = \(formatWithCommas(mainCombinations))")
print("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
*/