Project Euler Problem 493
70 coloured balls are placed in an urn, 10 for each of the seven rainbow colours.
Solution
Answer: 6.818741802
Let $X$ be the number of distinct colours among the 20 balls drawn.
We want:
$$\mathbb{E}[X]$$
There are 7 colours total, each appearing 10 times.
Step 1: Indicator variables
Define indicator variables
$$I_k = \begin{cases} 1 & \text{if colour } k \text{ appears at least once} \ 0 & \text{otherwise} \end{cases}$$
Then
$$X = I_1 + I_2 + \cdots + I_7$$
Using linearity of expectation:
$$\mathbb{E}[X] = \sum_{k=1}^7 \mathbb{E}[I_k] = 7\mathbb{E}[I_1]$$
since all colours are symmetric.
Now,
$$\mathbb{E}[I_1] = P(\text{a given colour appears})$$
So:
$$\mathbb{E}[X] = 7 \cdot P(\text{given colour appears})$$
Step 2: Compute the probability a colour appears
Instead of computing directly, compute the complement.
A particular colour is absent iff all 20 chosen balls come from the remaining 60 balls.
Total ways to choose 20 balls from 70:
$$\binom{70}{20}$$
Ways to choose 20 balls avoiding one colour:
$$\binom{60}{20}$$
Therefore
$$P(\text{given colour absent}) = \frac{\binom{60}{20}}{\binom{70}{20}}$$
Hence
$$P(\text{given colour appears}) = 1 - \frac{\binom{60}{20}}{\binom{70}{20}}$$
Thus
$$\boxed{ \mathbb{E}[X] = 7\left(1-\frac{\binom{60}{20}}{\binom{70}{20}}\right) }$$
Step 3: Numerical evaluation
We now evaluate
$$7\left(1-\frac{\binom{60}{20}}{\binom{70}{20}}\right)$$
Using exact arithmetic:
$$\frac{\binom{60}{20}}{\binom{70}{20}} = \prod_{k=0}^{19}\frac{60-k}{70-k}$$
Numerically,
$$\mathbb{E}[X] \approx 6.818741802$$
rounded to nine digits after the decimal point.
Python implementation
from math import comb
# Total ways to choose 20 balls from 70
total = comb(70, 20)
# Ways to choose 20 balls while avoiding one colour
without_colour = comb(60, 20)
# Expected number of distinct colours
expected = 7 * (1 - without_colour / total)
# Print with 9 digits after decimal point
print(f"{expected:.9f}")
Code walkthrough
from math import comb
Imports Python’s exact binomial coefficient function.
total = comb(70, 20)
Computes:
$$\binom{70}{20}$$
the total number of equally likely selections.
without_colour = comb(60, 20)
Computes:
$$\binom{60}{20}$$
the number of selections avoiding a particular colour.
expected = 7 * (1 - without_colour / total)
Implements:
$$7\left(1-\frac{\binom{60}{20}}{\binom{70}{20}}\right)$$
from the derivation above.
print(f"{expected:.9f}")
Prints the value rounded to exactly 9 decimal places.
Output:
6.818741802
Final verification
The result must lie between 1 and 7.
Since we draw 20 balls from 7 colours with many repetitions available, we should see almost all colours most of the time. Thus a value near 7 is reasonable.
The computed value:
$$6.818741802$$
fits this expectation.
Answer: 6.818741802