Project Euler Problem 100
If a box contains twenty-one coloured discs, composed of fifteen blue discs and six red discs, and two discs were taken
Solution
Answer: 756872327473
We seek integer solutions to the condition
$$P(\text{BB})=\frac12$$
where a box contains $b$ blue discs and $r$ red discs, with total
$$n=b+r.$$
The probability of drawing two blue discs without replacement is
$$\frac{b}{n}\cdot\frac{b-1}{n-1}=\frac12.$$
So we must solve
$$\frac{b(b-1)}{n(n-1)}=\frac12.$$
Equivalently,
$$2b(b-1)=n(n-1).$$
We are asked for the first solution with
$$n>10^{12}.$$
Step 1 — Transforming the equation
Start from
$$2b(b-1)=n(n-1).$$
Expand:
$$2b^2-2b=n^2-n.$$
Multiply by $4$:
$$8b^2-8b=4n^2-4n.$$
Now complete squares:
$$8b^2-8b+2=4n^2-4n+1.$$
Thus
$$2(2b-1)^2=(2n-1)^2+1.$$
Rearrange:
$$(2n-1)^2-2(2b-1)^2=-1.$$
Define
$$x=2n-1,\qquad y=2b-1.$$
Then we obtain the Pell equation
$$x^2-2y^2=-1.$$
Step 2 — Pell equation recurrence
The negative Pell equation
$$x^2-2y^2=-1$$
has infinitely many solutions generated recursively.
The first few are:
$$(1,1),\ (7,5),\ (41,29),\ (239,169),\dots$$
These correspond to:
$$n=\frac{x+1}{2},\qquad b=\frac{y+1}{2}.$$
For example:
- $x=239,\ y=169$
gives
$$n=120,\qquad b=85,$$
which matches the problem statement.
The standard recurrence for solutions is
$$x_{k+1}=3x_k+4y_k,$$
$$y_{k+1}=2x_k+3y_k.$$
Converting back to $(n,b)$, we get:
$$n_{k+1}=3n_k+4b_k-3,$$
$$b_{k+1}=2n_k+3b_k-2.$$
Starting from the known solution
$$(n,b)=(21,15),$$
we repeatedly generate larger solutions until $n>10^{12}$.
Step 3 — Python implementation
# Project Euler Problem 100
LIMIT = 10**12
# Initial known solution:
# total discs = 21
# blue discs = 15
n = 21
b = 15
# Generate larger solutions using the Pell recurrence
while n <= LIMIT:
n, b = (
3 * n + 4 * b - 3,
2 * n + 3 * b - 2
)
print(b)
Step 4 — Code walkthrough
LIMIT = 10**12
We need the first arrangement with more than one trillion total discs.
n = 21
b = 15
Start from the first nontrivial solution given in the problem.
while n <= LIMIT:
Keep generating solutions until the total exceeds $10^{12}$.
n, b = (
3 * n + 4 * b - 3,
2 * n + 3 * b - 2
)
This is the Pell-equation recurrence transformed back into $(n,b)$-coordinates.
Each iteration produces the next valid arrangement.
print(b)
Output the number of blue discs in the first valid arrangement exceeding the limit.
Step 5 — Checking small cases
Starting from $(n,b)=(21,15)$:
Next recurrence gives:
$$n=120,\qquad b=85.$$
Check:
$$\frac{85}{120}\cdot\frac{84}{119} = \frac{7140}{14280} = \frac12.$$
Correct.
Continuing the recurrence eventually yields:
$$n=1,070,379,110,497,$$
$$b=756,872,327,473.$$
Indeed,
$$n > 10^{12}.$$
Final verification
- The solution satisfies the Pell equation structure.
- The recurrence generates only exact integer solutions.
- The resulting total exceeds $10^{12}$.
- The blue count is slightly smaller than the total, as expected.
Answer: 756872327473