Project Euler Problem 394
Jeff eats a pie in an unusual way.
Solution
Answer: 3.2370342194
Let $E(x)$ denote the expected number of repetitions when the stopping threshold is
$$F=\frac1x.$$
We begin with the whole pie remaining, i.e. remaining fraction $=1$.
1. Mathematical analysis
Step 1: Distribution of the remaining piece
Suppose a fraction $r$ of the pie remains.
Jeff chooses two points independently and uniformly on the remaining boundary arc.
Parameterize the remaining arc by $[0,r]$, measured counterclockwise from the initial cut.
Let the two random cut positions be $U,V\sim \text{Uniform}(0,r)$.
The pie is divided into three sectors. Jeff eats the first two sectors counterclockwise from the initial cut, so the only part left is the sector from
$$\max(U,V)$$
to $r$.
Hence the new remaining fraction is
$$r' = r-\max(U,V).$$
Define
$$M=\frac{\max(U,V)}{r}.$$
Then $M\in[0,1]$ and
$$r' = r(1-M).$$
Step 2: Density of $M$
For two iid uniform variables on $[0,1]$,
$$\Pr(M\le m)=m^2.$$
Therefore,
$$f_M(m)=2m.$$
Let
$$T=1-M.$$
Then the remaining fraction multiplier is $T$, with density
$$f_T(t)=2(1-t), \qquad 0\le t\le1.$$
Thus after one step,
$$r' = rT.$$
Step 3: Integral equation for $E(x)$
We stop once the remaining fraction becomes $<1/x$.
Equivalently, starting from remaining fraction $1$, we continue while
$$r\ge \frac1x.$$
By scale invariance, the expected number of future steps depends only on $x$.
Conditioning on the first cut:
$$E(x) = 1+\int_0^1 E(xt),2(1-t),dt, \qquad x\ge1.$$
Also,
$$E(x)=0 \qquad (x<1).$$
2. Convert to a differential equation
Rewrite:
$$E(x)=1+2\int_0^1 E(xt)(1-t),dt.$$
Substitute $u=xt$:
$$E(x) = 1+\frac{2}{x}\int_0^x E(u),du -\frac{2}{x^2}\int_0^x uE(u),du.$$
Define
$$A(x)=\int_0^x E(u),du, \qquad B(x)=\int_0^x uE(u),du.$$
Then
$$E(x)=1+\frac{2A}{x}-\frac{2B}{x^2}.$$
Differentiate:
$$E'(x)=-\frac{2A}{x^2}+\frac{4B}{x^3}.$$
Using
$$A'(x)=E(x), \qquad B'(x)=xE(x),$$
one obtains after elimination:
$$x^2E''(x)+4xE'(x)-2=0.$$
3. Solve the ODE
Let
$$Y(x)=E'(x).$$
Then
$$x^2Y'+4xY-2=0,$$
or
$$Y'+\frac4xY=\frac2{x^2}.$$
Using integrating factor $x^4$:
$$(x^4Y)'=2x^2.$$
Integrate:
$$x^4Y=\frac23x^3+C,$$
so
$$E'(x)=\frac{2}{3x}+\frac{C}{x^4}.$$
Integrating again:
$$E(x)=\frac23\ln x-\frac{C}{3x^3}+D.$$
Step 4: Determine constants
We know
$$E(1)=1.$$
Also, from the integral equation, since $E(u)=0$ for $u<1$,
$$E'(1)=0.$$
Compute:
$$E'(1)=\frac23+C=0 \quad\Rightarrow\quad C=-\frac23.$$
Hence
$$E(x) = \frac23\ln x+\frac{2}{9x^3}+D.$$
Using $E(1)=1$:
$$1=0+\frac29+D$$
so
$$D=\frac79.$$
Therefore the exact formula is
$$\boxed{ E(x)=\frac23\ln x+\frac79+\frac{2}{9x^3} }.$$
4. Compute $E(40)$
$$E(40) = \frac23\ln(40) +\frac79 +\frac{2}{9\cdot40^3}.$$
Now evaluate numerically:
$$\ln(40)\approx 3.6888794541139363.$$
Thus
$$E(40) \approx \frac23(3.6888794541139363) +0.7777777777777778 +\frac{2}{576000}.$$
$$E(40) \approx 2.459252969409291 +0.7777777777777778 +0.000003472222222222.$$
$$E(40)\approx 3.237034219409291.$$
Rounded to 10 decimal places:
$$\boxed{3.2370342194}$$
5. Python implementation
import math
def E(x):
return (2/3) * math.log(x) + 7/9 + 2/(9*x**3)
print(f"{E(1):.10f}")
print(f"{E(2):.10f}")
print(f"{E(7.5):.10f}")
print(f"{E(40):.10f}")
6. Code walkthrough
import math
Imports logarithm support.
def E(x):
return (2/3) * math.log(x) + 7/9 + 2/(9*x**3)
Implements the closed-form formula derived above.
print(f"{E(1):.10f}")
Checks:
$$E(1)=1.$$
print(f"{E(2):.10f}")
Produces
$$1.2676536759\ldots$$
matching the statement.
print(f"{E(7.5):.10f}")
Produces
$$2.1215732071\ldots$$
again matching the problem statement.
print(f"{E(40):.10f}")
Computes the required value.
7. Final verification
-
Growth is logarithmic, which makes sense because each step removes a random positive fraction of the remaining pie.
-
The value $E(40)\approx 3.24$ is reasonable:
-
$E(7.5)\approx2.12$,
-
increasing the threshold to $40$ should require somewhat more than one extra expected step.
Everything is consistent.
Answer: 3.2370342194