project euler problem 493 https projecteuler netproblem493 70 coloured balls are placed in an urn 10 for each of the seven rainbow colours what is the expected number of distinct colours in 20 randomly picked balls give your answer with nine digits after the decimal point a bcdefghij this combinatorial problem can be solved by decomposing the problem into the following steps 1 calculate the total number of possible picking combinations combinations binomcoeff70 20 2 calculate the number of combinations with one colour missing missing binomcoeff60 20 3 calculate the probability of one colour missing missingprob missing combinations 4 calculate the probability of no colour missing nomissingprob 1 missingprob 5 calculate the expected number of distinct colours expected 7 nomissingprob references https en wikipedia orgwikibinomialcoefficient calculates the expected number of distinct colours solution10 5 669644129 solution30 6 985042712 calculates the expected number of distinct colours solution 10 5 669644129 solution 30 6 985042712
import math

BALLS_PER_COLOUR = 10
NUM_COLOURS = 7
NUM_BALLS = BALLS_PER_COLOUR * NUM_COLOURS


def solution(num_picks: int = 20) -> str:
    
    total = math.comb(NUM_BALLS, num_picks)
    missing_colour = math.comb(NUM_BALLS - BALLS_PER_COLOUR, num_picks)

    result = NUM_COLOURS * (1 - missing_colour / total)

    return f"{result:.9f}"


if __name__ == "__main__":
    print(solution(20))