Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,943 questions

55,787 answers

573 users

How to compute the total number of Powerball lottery combinations (5 out of 99 and 1 out of 26) in Ruby

1 Answer

0 votes
#
#   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
#

 



answered Jul 29 by avibootz

Related questions

...