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

1 Answer

0 votes
Imports System
				
'---------------------------------------------------------------
'   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
'---------------------------------------------------------------

Module 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 and floating‑point errors.
    '-----------------------------------------------------------
    Function Binomial(n As Integer, k As Integer) As Long
        If k > n Then Return 0
        If k > n - k Then k = n - k   ' symmetry: C(n,k) = C(n,n-k)

        Dim resultValue As Long = 1

        For i As Integer = 1 To k
            resultValue = resultValue * (n - i + 1) \ i
        Next

        Return resultValue
    End Function

    '-----------------------------------------------------------
    '   FormatWithCommas(value):
    '   Converts a Long into a comma‑formatted string.
    '
    '   Example:
    '       11238513 -> "11,238,513"
    '-----------------------------------------------------------
    Function FormatWithCommas(value As Long) As String
        Return value.ToString("N0")   ' built‑in formatting
    End Function

    Sub Main()
        Const MAIN_COUNT As Integer = 5
        Const MAIN_MAX As Integer = 69
        Const POWER_MAX As Integer = 26

        ' Compute C(69, 5)
        Dim mainComb As Long = Binomial(MAIN_MAX, MAIN_COUNT)

        ' Multiply by 26 Powerball choices
        Dim totalComb As Long = mainComb * POWER_MAX

        Console.WriteLine("Powerball combinations (5 out of 69 and 1 out of 26):")
        Console.WriteLine("C(69, 5) = " & FormatWithCommas(mainComb))
        Console.WriteLine("Total combinations = " & FormatWithCommas(totalComb))
    End Sub

End Module


' 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

...