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

Create your online store today with Shopify

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

Disclosure: My content contains affiliate links.

43,624 questions

56,911 answers

573 users

How to compute the total number of combinations when choosing 7 numbers out of 33 in VB.NET

1 Answer

0 votes
Imports System

Module TotalCombinations

    ''' <summary>
    ''' Computes the number of combinations C(n, r):
    '''
    '''     C(n, r) = n! / (r! * (n - r)!)
    '''
    ''' The calculation is performed incrementally to avoid
    ''' computing large factorials.
    ''' </summary>
    Private Function Combinations(n As Integer, r As Integer) As ULong

        ' Use symmetry: C(n, r) = C(n, n - r)
        If r > n - r Then
            r = n - r
        End If

        Dim result As ULong = 1UL

        '
        ' Compute:
        '
        '   n × (n - 1) × ... × (n - r + 1)
        '   --------------------------------
        '           1 × 2 × ... × r
        '
        ' Dividing at each step keeps intermediate values smaller.
        '
        For i As Integer = 0 To r - 1
            result *= CULng(n - i)
            result \= CULng(i + 1)   ' Integer division
        Next

        Return result

    End Function

    Sub Main()

        Const TotalNumbers As Integer = 33
        Const ChosenNumbers As Integer = 7

        Dim totalCombinations As ULong =
            Combinations(TotalNumbers, ChosenNumbers)

        Console.WriteLine(
            $"C({TotalNumbers}, {ChosenNumbers}) = {totalCombinations}")

    End Sub

End Module



' run:
'
' C(33, 7) = 4272048
' 

 



answered 1 day ago by avibootz

Related questions

...