#
# Compute Powerball lottery combinations:
# - Choose 5 distinct numbers out of 69
# - Choose 1 Powerball number out of 26
#
# Total combinations = C(69, 5) * 26
#
# This program:
# - Implements an efficient binomial coefficient function
# - Uses integer arithmetic only
# - Adds comma formatting for readability
#
#
# 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 and floating‑point inaccuracies.
#
def binomial(n, k)
return 0 if k > n
k = [k, n - k].min # symmetry: C(n, k) = C(n, n-k)
result = 1
(1..k).each do |i|
result = result * (n - i + 1) / i
end
result
end
#
# format_with_commas(value):
# Formats an integer with commas.
#
# Example:
# 11238513 -> "11,238,513"
#
def format_with_commas(value)
value.to_s.reverse.scan(/\d{1,3}/).join(",").reverse
end
MAIN_COUNT = 5
MAIN_MAX = 69
POWER_MAX = 26
# Compute C(69, 5)
main_combinations = binomial(MAIN_MAX, MAIN_COUNT)
# Multiply by 26 Powerball choices
total_combinations = main_combinations * POWER_MAX
puts "Powerball combinations (5 out of 69 and 1 out of 26):"
puts "C(69, 5) = #{format_with_commas(main_combinations)}"
puts "Total combinations = #{format_with_commas(total_combinations)}"
#
# run:
#
# Powerball combinations (5 out of 69 and 1 out of 26):
# C(69, 5) = 11,238,513
# Total combinations = 292,201,338
#