All Euler problems
Project Euler

Dice Game

Peter has nine four-sided (pyramidal) dice, each with faces numbered 1 to 4. Colin has six six-sided (cubic) dice, each with faces numbered 1 to 6. Peter and Colin roll their dice and compare total...

Source sync Apr 19, 2026
Problem #0205
Level Level 03
Solved By 16,470
Languages C++, Python
Answer 0.5731441
Length 338 words
probabilitygame_theorylinear_algebra

Problem Statement

This archive keeps the full statement, math, and original media on the page.

Peter has nine four-sided (pyramidal) dice, each with faces numbered \(1, 2, 3, 4\).

Colin has six six-sided (cubic) dice, each with faces numbered \(1, 2, 3, 4, 5, 6\).

Peter and Colin roll their dice and compare totals: the highest total wins. The result is a draw if the totals are equal.

What is the probability that Pyramidal Peter beats Cubic Colin? Give your answer rounded to seven decimal places in the form 0.abcdefg.

Problem 205: Dice Game

Mathematical Foundation

Theorem (Convolution of Discrete Uniform Distributions). Let X1,X2,,XnX_1, X_2, \ldots, X_n be independent random variables, each uniformly distributed on {1,2,,d}\{1, 2, \ldots, d\}. The probability mass function of Sn=i=1nXiS_n = \sum_{i=1}^{n} X_i is given by the nn-fold convolution:

fSn(s)=(fX1fX2fXn)(s)f_{S_n}(s) = (f_{X_1} * f_{X_2} * \cdots * f_{X_n})(s)

where fXi(k)=1/df_{X_i}(k) = 1/d for k{1,,d}k \in \{1, \ldots, d\} and 00 otherwise. Equivalently, the number of ways to write s=k1++kns = k_1 + \cdots + k_n with each ki{1,,d}k_i \in \{1, \ldots, d\} is the coefficient of xsx^s in (x+x2++xd)n(x + x^2 + \cdots + x^d)^n.

Proof. For independent discrete random variables, the PMF of the sum is the convolution of the individual PMFs. This follows from the law of total probability: P(Sn=s)=k=1dP(Sn1=sk)P(Xn=k)P(S_n = s) = \sum_{k=1}^{d} P(S_{n-1} = s - k) \cdot P(X_n = k), which is precisely the convolution formula. The generating function representation follows from the product rule for generating functions of independent variables. \square

Theorem (Winning Probability). Let PP denote Peter’s total (sum of 9 dice with d=4d=4) and CC denote Colin’s total (sum of 6 dice with d=6d=6). Then:

Pr(P>C)=14966p=936fP(p)FC(p1)\Pr(P > C) = \frac{1}{4^9 \cdot 6^6} \sum_{p=9}^{36} f_P(p) \cdot F_C(p-1)

where fP(p)f_P(p) is the number of outcomes giving Peter total pp, and FC(t)=c=6tfC(c)F_C(t) = \sum_{c=6}^{t} f_C(c) is the cumulative frequency function for Colin.

Proof. Since PP and CC are independent:

Pr(P>C)=pc<pPr(P=p)Pr(C=c)=p=936Pr(P=p)Pr(C<p).\Pr(P > C) = \sum_{p} \sum_{c < p} \Pr(P = p) \Pr(C = c) = \sum_{p=9}^{36} \Pr(P = p) \cdot \Pr(C < p).

Writing Pr(P=p)=fP(p)/49\Pr(P = p) = f_P(p)/4^9 and Pr(C<p)=Pr(Cp1)=FC(p1)/66\Pr(C < p) = \Pr(C \leq p-1) = F_C(p-1)/6^6, we obtain the claimed formula. \square

Lemma (Range Constraints). Peter’s sum ranges over [9,36][9, 36] (49=2621444^9 = 262144 total outcomes). Colin’s sum ranges over [6,36][6, 36] (66=466566^6 = 46656 total outcomes).

Proof. The minimum of nn dice each showing at least 1 is nn; the maximum with faces up to dd is ndnd. For Peter: n=9,d=4n=9, d=4 gives [9,36][9, 36]. For Colin: n=6,d=6n=6, d=6 gives [6,36][6, 36]. \square

Editorial

Peter has 9 four-sided dice; Colin has 6 six-sided dice. Find P(Peter beats Colin), rounded to 7 decimal places. Compute sum distributions via convolution, then sum over all (p, c) pairs where Peter’s total p > Colin’s total c. We compute f_P[9..36]: frequency distribution for sum of 9 four-sided dice. We then compute f_C[6..36]: frequency distribution for sum of 6 six-sided dice. Finally, compute cumulative F_C[t] = sum of f_C[6..t] for t = 6..36.

Pseudocode

Input: Peter has 9d4, Colin has 6d6
Output: P(Peter > Colin) rounded to 7 decimal places
Compute f_P[9..36]: frequency distribution for sum of 9 four-sided dice
Compute f_C[6..36]: frequency distribution for sum of 6 six-sided dice
Compute cumulative F_C[t] = sum of f_C[6..t] for t = 6..36
winning_count = sum over p = 9..36 of f_P[p] * F_C[p-1]
probability = winning_count / (4^9 * 6^6)
Return round(probability, 7)

Complexity Analysis

  • Time: O(nPdPsmax+nCdCsmax+smax)O(n_P \cdot d_P \cdot s_{\max} + n_C \cdot d_C \cdot s_{\max} + s_{\max}) for the convolutions and final summation, where smax=36s_{\max} = 36. This is O(9436+6636+36)=O(2628)O(9 \cdot 4 \cdot 36 + 6 \cdot 6 \cdot 36 + 36) = O(2628), effectively O(1)O(1).
  • Space: O(smax)=O(36)O(s_{\max}) = O(36), effectively O(1)O(1).

Answer

0.5731441\boxed{0.5731441}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_205/solution.cpp
#include <bits/stdc++.h>
using namespace std;

int main(){
    // Peter: 9 four-sided dice. Colin: 6 six-sided dice.
    // P(Peter > Colin) rounded to 7 decimal places.

    // Compute frequency distribution for sum of n dice with faces 1..f
    auto dice_dist = [](int n, int f) -> vector<long long> {
        int maxsum = n * f;
        vector<long long> freq(maxsum + 1, 0);
        freq[0] = 1;
        for(int die = 0; die < n; die++){
            vector<long long> nf(maxsum + 1, 0);
            for(int s = 0; s <= maxsum; s++){
                if(freq[s] == 0) continue;
                for(int face = 1; face <= f && s + face <= maxsum; face++){
                    nf[s + face] += freq[s];
                }
            }
            freq = nf;
        }
        return freq;
    };

    auto peter = dice_dist(9, 4);  // indices 0..36, nonzero for 9..36
    auto colin = dice_dist(6, 6);  // indices 0..36, nonzero for 6..36

    // Cumulative sum for Colin
    vector<long long> colin_cum(37, 0);
    for(int s = 1; s <= 36; s++){
        colin_cum[s] = colin_cum[s-1] + colin[s];
    }

    // P(Peter > Colin) = sum over p of peter[p] * colin_cum[p-1]
    long long win = 0;
    for(int p = 9; p <= 36; p++){
        win += peter[p] * colin_cum[p-1];
    }

    double total = (double)(1LL << 18) * 46656.0; // 4^9 * 6^6 = 262144 * 46656
    double prob = (double)win / (262144.0 * 46656.0);

    cout << fixed << setprecision(7) << prob << endl;
    return 0;
}