Project Euler Problem 389

An unbiased single 4-sided die is thrown and its value, T, is noted.

Project Euler Problem 389

Solution

Answer: 2406376.3623

Let

  • $T$ = result of one 4-sided die,
  • $C$ = sum of $T$ independent 6-sided dice,
  • $O$ = sum of $C$ independent 8-sided dice,
  • $D$ = sum of $O$ independent 12-sided dice,
  • $I$ = sum of $D$ independent 20-sided dice.

We are asked for $\mathrm{Var}(I)$.

The key tool is the law of total expectation and law of total variance.

For a fair $n$-sided die taking values $1,\dots,n$,

$$\mathbb E[X] = \frac{n+1}{2}, \qquad \mathrm{Var}(X)=\frac{n^2-1}{12}.$$

We will repeatedly use the following fact.

If $S_N$ is the sum of $N$ iid random variables $X_i$, where $N$ is itself random and independent of the $X_i$, then

$$\mathbb E[S_N] = \mathbb E[N]\mathbb E[X],$$

and

$$\mathrm{Var}(S_N) = \mathbb E[N]\mathrm{Var}(X) + \mathrm{Var}(N)\bigl(\mathbb E[X]\bigr)^2.$$

That identity comes directly from

$$\mathrm{Var}(S_N) = \mathbb E[\mathrm{Var}(S_N\mid N)] + \mathrm{Var}(\mathbb E[S_N\mid N]).$$


Step 1: Distribution parameters for each die

For a $k$-sided die:

$$\mu_k = \frac{k+1}{2}, \qquad \sigma_k^2 = \frac{k^2-1}{12}.$$

Thus:

Die Mean Variance
d4 $5/2$ $5/4$
d6 $7/2$ $35/12$
d8 $9/2$ $21/4$
d12 $13/2$ $143/12$
d20 $21/2$ $133/4$

Step 2: Compute $C$

Conditioned on $T=t$,

$$C = \sum_{i=1}^t X_i,$$

where $X_i$ are iid d6 rolls.

Hence

$$\mathbb E[C] = \mathbb E[T]\mu_6 = \frac52\cdot\frac72 = \frac{35}{4}.$$

Using total variance:

$$\mathrm{Var}(C) = \mathbb E[T]\sigma_6^2 + \mathrm{Var}(T)\mu_6^2.$$

So

$$\mathrm{Var}(C) = \frac52\cdot\frac{35}{12} + \frac54\cdot\left(\frac72\right)^2.$$

Compute:

$$= \frac{175}{24} + \frac{245}{16} = \frac{595}{24}.$$


Step 3: Compute $O$

$O$ is the sum of $C$ iid d8 rolls.

Mean:

$$\mathbb E[O] = \mathbb E[C]\mu_8 = \frac{35}{4}\cdot\frac92 = \frac{315}{8}.$$

Variance:

$$\mathrm{Var}(O) = \mathbb E[C]\sigma_8^2 + \mathrm{Var}(C)\mu_8^2.$$

Thus

$$= \frac{35}{4}\cdot\frac{21}{4} + \frac{595}{24}\cdot\left(\frac92\right)^2.$$

$$= \frac{735}{16} + \frac{16065}{96} = \frac{20475}{96} = \frac{6825}{32}.$$


Step 4: Compute $D$

$D$ is the sum of $O$ iid d12 rolls.

Mean:

$$\mathbb E[D] = \frac{315}{8}\cdot\frac{13}{2} = \frac{4095}{16}.$$

Variance:

$$\mathrm{Var}(D) = \mathbb E[O]\sigma_{12}^2 + \mathrm{Var}(O)\mu_{12}^2.$$

So

$$= \frac{315}{8}\cdot\frac{143}{12} + \frac{6825}{32}\cdot\left(\frac{13}{2}\right)^2.$$

$$= \frac{45045}{96} + \frac{1153425}{128}.$$

Common denominator $384$:

$$= \frac{180180 + 3460275}{384} = \frac{3640455}{384}.$$


Step 5: Compute $I$

$I$ is the sum of $D$ iid d20 rolls.

We need only the variance:

$$\mathrm{Var}(I) = \mathbb E[D]\sigma_{20}^2 + \mathrm{Var}(D)\mu_{20}^2.$$

Substitute values:

$$= \frac{4095}{16}\cdot\frac{133}{4} + \frac{3640455}{384}\cdot\left(\frac{21}{2}\right)^2.$$

First term:

$$\frac{4095\cdot133}{64} = \frac{544635}{64}.$$

Second term:

$$\frac{3640455}{384}\cdot\frac{441}{4} = \frac{1603440655}{1536}.$$

Convert first term to denominator $1536$:

$$\frac{544635}{64} = \frac{13071240}{1536}.$$

Hence

$$\mathrm{Var}(I) = \frac{1616511895}{1536}.$$

Decimal value:

$$\mathrm{Var}(I) = 1052416.5983072916\ldots$$

Rounded to 4 decimal places:

$$1052416.5983$$


Python implementation

from fractions import Fraction

def die_stats(sides):
    """
    Return (mean, variance) for a fair die with values 1..sides.
    """
    mean = Fraction(sides + 1, 2)
    var = Fraction(sides * sides - 1, 12)
    return mean, var

def compound_sum(mean_n, var_n, mean_x, var_x):
    """
    If S is the sum of N iid X variables:

        S = X1 + X2 + ... + XN

    where N is random and independent of the Xi, then

        E[S]   = E[N] E[X]
        Var[S] = E[N] Var[X] + Var[N] (E[X])^2

    Returns (mean_S, var_S).
    """
    mean_s = mean_n * mean_x
    var_s = mean_n * var_x + var_n * mean_x * mean_x
    return mean_s, var_s

# Start with T: one d4
mean_T, var_T = die_stats(4)

# C: sum of T d6
mean_d6, var_d6 = die_stats(6)
mean_C, var_C = compound_sum(mean_T, var_T, mean_d6, var_d6)

# O: sum of C d8
mean_d8, var_d8 = die_stats(8)
mean_O, var_O = compound_sum(mean_C, var_C, mean_d8, var_d8)

# D: sum of O d12
mean_d12, var_d12 = die_stats(12)
mean_D, var_D = compound_sum(mean_O, var_O, mean_d12, var_d12)

# I: sum of D d20
mean_d20, var_d20 = die_stats(20)
mean_I, var_I = compound_sum(mean_D, var_D, mean_d20, var_d20)

print("Exact variance:", var_I)
print("Decimal:", float(var_I))
print("Rounded:", round(float(var_I), 4))

Code walkthrough

die_stats

Computes the mean and variance of a fair die:

$$\mu=\frac{n+1}{2},\qquad \sigma^2=\frac{n^2-1}{12}.$$

Using Fraction keeps everything exact.


compound_sum

Implements the fundamental formulas:

$$E[S]=E[N]E[X]$$

and

$$\mathrm{Var}(S) = E[N]\mathrm{Var}(X) + \mathrm{Var}(N)(E[X])^2.$$

This is applied repeatedly through the chain:

$$T \to C \to O \to D \to I.$$


Recursive computation

Each stage becomes the random number of dice rolled in the next stage.

For example:

mean_C, var_C = compound_sum(mean_T, var_T, mean_d6, var_d6)

computes the statistics of the sum of $T$ six-sided dice.


Final verification

The variance grows very rapidly because each layer multiplies both the expected count and the uncertainty from the previous layer.

The final value is about $1.05\times10^6$, which is reasonable given the expected number of d20 rolls is

$$E[D] = \frac{4095}{16} \approx 256.$$

A sum of roughly 256 d20 dice already has variance around

$$256 \cdot \frac{133}{4} \approx 8500,$$

and the randomness in the number of dice rolled contributes vastly more, pushing the variance above a million.

Answer: 1052416.5983