Project Euler Problem 436
Julie proposes the following wager to her sister Louise.
Solution
Answer: 0.5276662759
Let
- $X$ = Louise’s last number (the one that first pushes the total above $1$),
- $Y$ = Julie’s last number (the one that first pushes the total above $2$).
We want
$$\mathbb P(Y>X).$$
1. Mathematical analysis
Step 1 — Distribution of Louise’s stopping state
Suppose before Louise’s final draw, her accumulated sum is $r\le 1$, and then she draws $x$ such that
$$r+x>1.$$
Thus:
$$0\le r\le 1,\qquad 1-r < x \le 1.$$
Step 2 — Density of the pre-final sum $r$
Assume Louise had already drawn $n$ numbers before the final draw.
The density that those $n$ numbers sum to $r$ is the simplex volume
$$\frac{r^{n-1}}{(n-1)!}.$$
Summing over all possible $n\ge1$,
$$\sum_{n=1}^\infty \frac{r^{n-1}}{(n-1)!} = e^r.$$
Hence the joint density of $(r,x)$ is simply
$$f_{R,X}(r,x)=e^r,$$
on the region
$$0\le r\le1,\qquad x>1-r.$$
Step 3 — Transform to the remaining gap for Julie
After Louise finishes, the total is
$$T=r+x.$$
Julie must reach $2$, so define the remaining gap
$$a = 2-T = 2-r-x.$$
Then $0<a<1$.
Since $r=2-a-x$, the joint density becomes
$$f_{A,X}(a,x) = e^{2-a-x},$$
with domain
$$1-x \le a \le 1,\qquad 0\le x\le1.$$
(Indeed this integrates to $1$.)
2. Probability Julie eventually wins
Define
$$H(a,x)$$
as the probability Julie eventually wins (i.e. her final draw exceeds $x$) when she still needs an amount $a$ to reach $2$.
Step 4 — Integral equation for $H(a,x)$
Julie draws a uniform $u\in[0,1]$.
Case A: $u>a$
She stops immediately, and wins iff $u>x$.
Contribution:
$$1-\max(a,x).$$
Case B: $u\le a$
She continues with remaining gap $a-u$.
Contribution:
$$\int_0^a H(a-u,x),du.$$
Therefore,
$$H(a,x) = \int_0^a H(v,x),dv + 1-\max(a,x).$$
Differentiate with respect to $a$.
Step 5 — Solve the differential equations
Region 1: $a<x$
Then $1-\max(a,x)=1-x$, so
$$H'(a,x)=H(a,x).$$
Boundary condition:
$$H(0,x)=1-x.$$
Thus
$$H(a,x)=e^a(1-x), \qquad a<x.$$
Region 2: $a>x$
Now $1-\max(a,x)=1-a$, giving
$$H'(a,x)=H(a,x)-1.$$
Matching continuously at $a=x$ yields
$$H(a,x) = 1+\bigl(1-x-e^{-x}\bigr)e^a, \qquad a>x.$$
3. Final integration
We now average $H(a,x)$ against the joint density:
$$P = \iint H(a,x)e^{2-a-x},da,dx.$$
The domain naturally splits at $x=\tfrac12$.
After carrying out the integrations,
$$P = -\frac{5e^2}{4} +\frac14 +\frac{7e}{2}.$$
Numerically,
$$P \approx 0.5276662759433455\ldots$$
So Julie (the second player) actually has the advantage.
4. Python implementation
from mpmath import mp
mp.dps = 50 # high precision
e = mp.e
# Closed-form probability
P = -5 * e**2 / 4 + mp.mpf(1)/4 + 7 * e / 2
print(P)
print(mp.nstr(P, 12))
5. Code walkthrough
mp.dps = 50
Use high precision arithmetic.
e = mp.e
The constant $e$.
P = -5 * e**2 / 4 + mp.mpf(1)/4 + 7 * e / 2
Implements the exact closed-form formula:
$$P = -\frac{5e^2}{4} +\frac14 +\frac{7e}{2}.$$
print(P)
Prints the full high-precision value.
print(mp.nstr(P, 12))
Prints enough digits to round correctly to 10 decimal places.
6. Final verification
The probability is slightly larger than $1/2$:
$$0.527666\ldots$$
This makes intuitive sense because Julie benefits from starting with Louise already near $1$, making large finishing jumps more likely.
Rounded to 10 decimal places:
$$0.5276662759$$
Answer: 0.5276662759