/*
This program computes the total number of lottery combinations for:
- choosing 6 numbers out of 37
- choosing 1 power number out of 7
Total combinations = C(37,6) * C(7,1)
We implement an idiomatic binomial coefficient function using the
multiplicative formula:
C(n, k) = product(i = 1..k) of (n - k + i) / i
Why this method?
- Avoids huge factorials (37! is far too large for Long)
- Keeps intermediate values small and exact
- Efficient, clean, and idiomatic Scala
*/
object LotteryCombinations {
def binomialCoefficient(n: Long, k: Long): Long = {
if (k > n) return 0
// Use symmetry: C(n, k) == C(n, n-k)
val kk = if (k > n - k) n - k else k
var result: Long = 1
for (i <- 1L to kk) {
result = result * (n - kk + i) / i
}
result
}
def main(args: Array[String]): Unit = {
val mainN: Long = 37
val mainK: Long = 6
val powerN: Long = 7
val powerK: Long = 1
val mainCombos: Long = binomialCoefficient(mainN, mainK)
val powerCombos: Long = binomialCoefficient(powerN, powerK)
val total: Long = mainCombos * powerCombos
println(s"Main combinations (C(37,6)): $mainCombos")
println(s"Power combinations (C(7,1)): $powerCombos")
println(s"Total lottery combinations: $total")
}
}
/*
run:
Main combinations (C(37,6)): 2324784
Power combinations (C(7,1)): 7
Total lottery combinations: 16273488
*/