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

55,449 answers

573 users

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

1 Answer

0 votes
import java.math.BigInteger

/*
    This program computes the total number of Powerball lottery combinations:
        - Choose 5 distinct numbers out of 69
        - Choose 1 Powerball number out of 26

    Total combinations = C(69, 5) * 26

    It uses:
        - BigInteger for safe integer arithmetic
        - an efficient binomial coefficient function
        - a helper to format numbers with commas
*/

/*
    binomial(n, k):
    Computes C(n, k) using the multiplicative formula:

        C(n, k) = Π (n - i + 1) / i   for i = 1..k

    Implemented using BigInteger to avoid overflow.
*/
fun binomial(n: Int, k: Int): BigInteger {
    if (k > n) return BigInteger.ZERO

    val kk = minOf(k, n - k)   // symmetry: C(n, k) = C(n, n-k)
    var result = BigInteger.ONE

    for (i in 1..kk) {
        result = result * BigInteger.valueOf((n - i + 1).toLong()) /
                 BigInteger.valueOf(i.toLong())
    }

    return result
}

/*
    formatWithCommas(value):
    Formats a BigInteger with commas.
*/
fun formatWithCommas(value: BigInteger): String {
    val s = value.toString()
    return s.reversed().chunked(3).joinToString(",").reversed()
}

fun main() {
    val MAIN_COUNT = 5
    val MAIN_MAX   = 69
    val POWER_MAX  = BigInteger.valueOf(26)

    // Compute C(69, 5)
    val mainCombinations: BigInteger = binomial(MAIN_MAX, MAIN_COUNT)

    // Multiply by 26 Powerball choices
    val totalCombinations: BigInteger = mainCombinations * POWER_MAX

    println("Powerball combinations (5 out of 69 and 1 out of 26):")
    println("C(69, 5) = ${formatWithCommas(mainCombinations)}")
    println("Total combinations = ${formatWithCommas(totalCombinations)}")
}



/*
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

...