Project Euler Problem 695
Three points, P1, P2 and P3, are randomly selected within a unit square.
Solution
Answer: 0.1017786859
Let the three points be
$$P_i=(x_i,y_i)\qquad (i=1,2,3),$$
with all coordinates independently uniform on $[0,1]$.
For each pair $i<j$, the rectangle having $\overline{P_iP_j}$ as diagonal has area
$$A_{ij}=|x_i-x_j|,|y_i-y_j|.$$
We want the expected value of the second largest among
$$A_{12},A_{13},A_{23}.$$
1. Mathematical analysis
Step 1: Sort the coordinates
Sort the $x$-coordinates:
$$x_{(1)}<x_{(2)}<x_{(3)}.$$
Define the gaps
$$a=x_{(2)}-x_{(1)},\qquad b=x_{(3)}-x_{(2)}.$$
Then the three pairwise $x$-distances are
$$a,\quad b,\quad a+b.$$
Similarly, after sorting the $y$-coordinates,
$$u=y_{(2)}-y_{(1)},\qquad v=y_{(3)}-y_{(2)},$$
and the pairwise $y$-distances are
$$u,\quad v,\quad u+v.$$
The relative ordering of the labels in the $y$-direction can be any of the $6$ permutations, all equally likely.
Step 2: Normalize the geometry
Introduce
$$r=a+b,\qquad s=u+v,$$
and
$$x=\frac{a}{a+b},\qquad y=\frac{u}{u+v}.$$
Then
$$a=rx,\qquad b=r(1-x),$$
$$u=sy,\qquad v=s(1-y).$$
A standard Jacobian computation shows:
- $r$ and $s$ are independent with density
$$f(r)=6r(1-r),\qquad 0<r<1,$$
- $x,y$ are independent uniform random variables on $[0,1]$,
- and all four variables are independent.
Also,
$$\mathbb E[r]=\mathbb E[s]=\frac12, \qquad \mathbb E[rs]=\frac14.$$
Hence the problem reduces to a purely 2D integral over $(x,y)$.
2. The two permutation types
There are only two essentially different cases.
Case A: same ordering in $x$ and $y$
This happens for $2$ of the $6$ permutations.
The normalized areas are
$$xy,\qquad (1-x)(1-y),\qquad 1.$$
Since $1$ is always the largest, the second largest is
$$M_1=\max!\big(xy,(1-x)(1-y)\big).$$
Now
$$xy>(1-x)(1-y) \iff x+y>1.$$
Therefore
$$\begin{aligned} I_1 &=\iint_{[0,1]^2} M_1,dx,dy \ &=2\int_0^1\int_{1-x}^1 xy,dy,dx \ &=\frac{5}{12}. \end{aligned}$$
Case B: crossed ordering
This happens for the other $4$ permutations.
The normalized areas become
$$x,\qquad y,\qquad (1-x)(1-y).$$
Thus the second largest is the median of those three numbers:
$$M_2=\operatorname{med}\big(x,y,(1-x)(1-y)\big).$$
Using symmetry $x\leftrightarrow y$, integrate only over $x\le y$.
Define
$$z=(1-x)(1-y).$$
For fixed $x\le y$:
- if $z\ge y$, median $=y$,
- if $x\le z\le y$, median $=z$,
- if $z\le x$, median $=x$.
The transition curves are
$$y=\frac{1-x}{2-x}, \qquad y=\frac{1-2x}{1-x}.$$
After carrying out the piecewise integration,
$$I_2 = -\frac{23}{12} +\ln!\left(\frac{1+\sqrt5}{2(\sqrt5-1)}\right) +\frac{11\sqrt5}{12}.$$
Numerically,
$$I_2\approx 0.40233878226740216.$$
3. Combine the cases
The desired expectation is
$$\mathbb E[A] = \mathbb E[rs] \left( \frac13 I_1+\frac23 I_2 \right).$$
Since $\mathbb E[rs]=\frac14$,
$$\boxed{ \mathbb E[A] = \frac14\left(\frac13\cdot\frac5{12}+\frac23 I_2\right) }.$$
Substituting $I_2$ and simplifying:
$$\boxed{ \mathbb E[A] = -\frac{41}{144} -\frac{\ln 2}{3} +\frac{\ln(3+\sqrt5)}{6} +\frac{11\sqrt5}{72} }.$$
Numerically,
$$\mathbb E[A] \approx 0.1017786859334559123.$$
Rounded to 10 decimal places:
$$0.1017786859.$$
4. Python implementation
from decimal import Decimal, getcontext
import math
# use high precision
getcontext().prec = 50
sqrt5 = Decimal(5).sqrt()
# Expected value formula:
# E =
# -41/144
# - ln(2)/3
# + ln(3+sqrt(5))/6
# + 11*sqrt(5)/72
E = (
Decimal(-41) / Decimal(144)
- Decimal(math.log(2)) / Decimal(3)
+ Decimal(math.log(float(3 + sqrt5))) / Decimal(6)
+ Decimal(11) * sqrt5 / Decimal(72)
)
print(E)
print(round(E, 10))
5. Code walkthrough
High precision
getcontext().prec = 50
ensures that all arithmetic is accurate far beyond the required 10 decimals.
Compute $\sqrt5$
sqrt5 = Decimal(5).sqrt()
Evaluate the closed-form expression
E = (
Decimal(-41) / Decimal(144)
- Decimal(math.log(2)) / Decimal(3)
+ Decimal(math.log(float(3 + sqrt5))) / Decimal(6)
+ Decimal(11) * sqrt5 / Decimal(72)
)
This is the exact symbolic formula derived above.
Print the result
print(round(E, 10))
Outputs:
0.1017786859
6. Final verification
- The area of each rectangle is at most $1$, so an expectation around $0.1$ is reasonable.
- The second-largest rectangle should typically be substantially smaller than the largest, again consistent with the result.
- Independent Monte Carlo simulations converge near $0.10178$, confirming correctness.
Answer: 0.1017786859