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,227 questions

56,129 answers

573 users

How to compute the total number of lottery combinations for choosing 6 out of 37 and 1 power out of 7 in Go

1 Answer

0 votes
package main

import (
    "fmt"
)

/*
    binomialCoefficient(n, k):
    Computes "n choose k" 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 64‑bit integers)
        - Keeps intermediate values small and exact
        - Efficient, clean, and idiomatic Go

    Returns:
        The binomial coefficient as uint64.
*/
func binomialCoefficient(n, k uint64) uint64 {
    if k > n {
        return 0
    }

    // Use symmetry: C(n, k) == C(n, n-k)
    if k > n-k {
        k = n - k
    }

    result := uint64(1)

    for i := uint64(1); i <= k; i++ {
        result = result * (n - k + i) / i
    }

    return result
}

func main() {
    var mainN uint64 = 37
    var mainK uint64 = 6

    var powerN uint64 = 7
    var powerK uint64 = 1

    mainCombos := binomialCoefficient(mainN, mainK)
    powerCombos := binomialCoefficient(powerN, powerK)

    total := mainCombos * powerCombos

    fmt.Println("Main combinations (C(37,6)):", mainCombos)
    fmt.Println("Power combinations (C(7,1)):", powerCombos)
    fmt.Println("Total lottery combinations:", total)
}


/*
run:

Main combinations (C(37,6)): 2324784
Power combinations (C(7,1)): 7
Total lottery combinations: 16273488

*/

 



answered Jul 27 by avibootz

Related questions

...