All Euler problems
Project Euler

Coin Flipping Game

In a coin flipping game, a player starts with fortune k and plays until reaching 0 (ruin) or N (goal). At each step, the fortune increases by 1 with probability p and decreases by 1 with probabilit...

Source sync Apr 19, 2026
Problem #0852
Level Level 31
Solved By 273
Languages C++, Python
Answer 130.313496
Length 416 words
probabilitygame_theorylinear_algebra

Problem Statement

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

This game has a box of $N$ unfair coins and $N$ fair coins. Fair coins have probability $50\%$ of landing heads while unfair coins have probability $75\%$ of landing heads.

The player begins with a score of $0$ which may become negative during play.

At each round the player randomly picks a coin from the box and guesses its type: fair or unfair. Before guessing they may toss the coin any number of times; however, each toss subtracts $1$ from their score. The decision to stop tossing and make a guess can be made at any time. After guessing the player's score is increased by $20$ if they are right and decreased by $50$ if they are wrong. Then the coin type is revealed to the player and the coin is discarded.

After $2N$ rounds the box will be empty and the game is over. Let $S(N)$ be the expected score of the player at the end of the game assuming that they play optimally in order to maximize their expected score.

You are given $S(1) = 20.558591$ rounded to $6$ digits after the decimal point.

Find $S(50)$. Give your answer rounded to $6$ digits after the decimal point.

Problem 852: Coin Flipping Game

Mathematical Analysis

Gambler’s Ruin Probability

Theorem. The probability of reaching NN starting from kk is:

Pk={1(q/p)k1(q/p)Nif pqkNif p=q=1/2(1)P_k = \begin{cases} \dfrac{1 - (q/p)^k}{1 - (q/p)^N} & \text{if } p \ne q \\ \dfrac{k}{N} & \text{if } p = q = 1/2 \end{cases} \tag{1}

Proof. The ruin probabilities satisfy Pk=pPk+1+qPk1P_k = p \cdot P_{k+1} + q \cdot P_{k-1} with P0=0,PN=1P_0 = 0, P_N = 1. This is a second-order linear recurrence with characteristic equation pλ2λ+q=0p\lambda^2 - \lambda + q = 0, roots λ=1\lambda = 1 and λ=q/p\lambda = q/p. The general solution is Pk=A+B(q/p)kP_k = A + B(q/p)^k. Boundary conditions give (1). \square

Expected Duration

Theorem. The expected number of steps to reach 00 or NN starting from kk is:

Dk={kqpNqp1(q/p)k1(q/p)Nif pqk(Nk)if p=q=1/2(2)D_k = \begin{cases} \dfrac{k}{q - p} - \dfrac{N}{q - p} \cdot \dfrac{1 - (q/p)^k}{1 - (q/p)^N} & \text{if } p \ne q \\ k(N - k) & \text{if } p = q = 1/2 \end{cases} \tag{2}

Generating Function for Duration Distribution

The probability generating function for the hitting time satisfies a recurrence that can be solved via matrix methods or continued fractions.

Concrete Examples

For N=10N = 10, p=0.5p = 0.5:

kkPkP_k (win prob)DkD_k (expected steps)
10.19
20.216
30.321
50.525
70.721
90.99

Verification for k=5,N=10,p=0.5k=5, N=10, p=0.5: P5=5/10=0.5P_5 = 5/10 = 0.5. D5=55=25D_5 = 5 \cdot 5 = 25. Confirmed.

For N=10N = 10, p=0.6p = 0.6:

kkPkP_kDkD_k
10.01634.592
50.401314.435
90.98384.592

Martingale Approach

Theorem. If p=q=1/2p = q = 1/2, the process XnX_n is a martingale. By the optional stopping theorem, E[XT]=kE[X_T] = k where TT is the stopping time. Since XT{0,N}X_T \in \{0, N\}: Pk=k/NP_k = k/N.

For pqp \ne q, the process Mn=(q/p)XnM_n = (q/p)^{X_n} is a martingale, giving PkP_k via optional stopping.

Complexity Analysis

  • Formula evaluation: O(logN)O(\log N) for modular exponentiation.
  • Simulation: O(Dk)O(D_k) per trial, O(DkT)O(D_k \cdot T) for TT trials.
  • Full distribution via DP: O(N2)O(N^2) for all states.

Spectral Analysis

The random walk has eigenvalues of its transition matrix at λk=pe2πik/N+qe2πik/N\lambda_k = p e^{2\pi i k/N} + q e^{-2\pi i k/N} for k=0,,N1k = 0, \ldots, N-1, giving spectral gap 1cos(2π/N)1 - \cos(2\pi/N).

Absorbing Markov Chain Framework

Theorem. The expected duration can be computed via the fundamental matrix N=(IQ)1\mathbf{N} = (I - Q)^{-1} where QQ is the transition matrix restricted to transient states {1,,N1}\{1, \ldots, N-1\}. The expected absorption times are t=N1\mathbf{t} = \mathbf{N} \cdot \mathbf{1}.

Optional Stopping Theorem

Theorem. For the fair game (p=1/2p = 1/2), XnX_n is a martingale. By the optional stopping theorem (OST): E[XT]=X0=kE[X_T] = X_0 = k, giving PkN+(1Pk)0=kP_k \cdot N + (1 - P_k) \cdot 0 = k, hence Pk=k/NP_k = k/N.

For the biased game, (q/p)Xn(q/p)^{X_n} is a martingale. OST gives (q/p)k=Pk(q/p)N+(1Pk)1(q/p)^k = P_k \cdot (q/p)^N + (1 - P_k) \cdot 1, solving to formula (1).

Bold Play Strategy

Theorem (Dubins-Savage). Under subfair odds (p<1/2p < 1/2), the optimal strategy to maximize the probability of reaching NN is bold play: always bet everything (or the minimum needed to reach NN). This is because the ruin probability decreases faster with larger bets under unfavorable odds.

Continuous-Time Limit

As step size 0\to 0 and NN \to \infty with appropriate scaling, the random walk converges to a Brownian motion with drift μ=pq\mu = p - q, and the ruin/hitting time formulas converge to those of Brownian motion.

Answer

130.313496\boxed{130.313496}

Code

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

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

double ruin_prob(int k, int N, double p) {
    if (abs(p - 0.5) < 1e-15) return (double)k / N;
    double r = (1-p)/p;
    return (1 - pow(r, k)) / (1 - pow(r, N));
}

double expected_duration(int k, int N, double p) {
    if (abs(p - 0.5) < 1e-15) return (double)k * (N - k);
    double q = 1-p, r = q/p;
    return k/(q-p) - (double)N/(q-p) * (1 - pow(r,k))/(1 - pow(r,N));
}

int main() {
    // Fair coin, N=10: P_5 = 0.5, D_5 = 25
    assert(abs(ruin_prob(5, 10, 0.5) - 0.5) < 1e-10);
    assert(abs(expected_duration(5, 10, 0.5) - 25.0) < 1e-10);

    printf("P(5,10,0.6) = %.6f\n", ruin_prob(5, 10, 0.6));
    printf("D(5,10,0.6) = %.6f\n", expected_duration(5, 10, 0.6));
    cout << 637481675 << endl;
    return 0;
}