Arranged Probability
A box contains n coloured discs, of which b are blue. When two discs are drawn at random without replacement, the probability of drawing two blue discs is exactly 1/2: (b)/(n) * (b-1)/(n-1) = (1)/(...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
If a box contains twenty-one coloured discs, composed of fifteen blue discs and six red discs, and two discs were taken at random, it can be seen that the probability of taking two blue discs, \(P(\text {BB}) = (15/21) \times (14/20) = 1/2\).
The next such arrangement, for which there is exactly \(50\%\) chance of taking two blue discs at random, is a box containing eighty-five blue discs and thirty-five red discs.
By finding the first arrangement to contain over \(10^{12} = 1\,000\,000\,000\,000\) discs in total, determine the number of blue discs that the box would contain.
Problem 100: Arranged Probability
Mathematical Foundation
Theorem 1 (Diophantine formulation). The condition is equivalent to the Diophantine equation
Proof. Cross-multiplying: , i.e., .
Theorem 2 (Reduction to Pell’s equation). Under the substitution , , equation (1) transforms to
Proof. From (1): . With and :
Remark. Equation (2) is the negative Pell equation . It has solutions because has an odd-length period in its continued fraction expansion.
Theorem 3 (Fundamental solution and recurrence). Equation has fundamental solution . All positive solutions are generated by
Proof. Verification that satisfies . For the recurrence, suppose . Then:
The recurrence arises from multiplication in : the fundamental solution to corresponds to the unit , and iterating generates all solutions to . The matrix form
corresponds to squaring the fundamental unit: , giving , .
Completeness (that all solutions arise this way) follows from the theory of Pell equations: the group of units of with norm is generated by .
Theorem 4 (Integrality of and ). For each solution of (2), both and are odd, so and are positive integers.
Proof. By induction. Base: and are both odd. Inductive step: if and are odd, then , and .
Theorem 5 (Second-order recurrence for and ). The sequences and satisfy the second-order linear recurrence
with initial values and .
Proof. From the matrix recurrence in Theorem 3, composing two steps of the recurrence and substituting , :
The characteristic equation of the matrix is (trace , determinant ). Therefore and . Substituting :
The same derivation applies to . Initial values: gives ; gives . Verification: .
Verification.
| Check: | |||
|---|---|---|---|
| 0 | 1 | 1 | |
| 1 | 3 | 4 | |
| 2 | 15 | 21 | |
| 3 | 85 | 120 |
Editorial
The probability condition is awkward in directly, but after the standard substitution it becomes a Pell-type equation. That means the valid arrangements do not need to be searched combinatorially; they appear as a recurrence sequence of exact solutions.
The implementation works entirely in the recurrence derived from the Pell equation. Starting from the first two nontrivial solutions, it repeatedly generates the next arrangement and stops when the total number of discs first exceeds . So the search space is explored in the exact order of valid solutions, and no invalid pair is ever considered.
Pseudocode
Initialize the first two solutions of the blue-disc recurrence.
While the current total number of discs does not yet exceed the threshold:
Compute the next blue-disc count from the recurrence
Compute the next total-disc count from the same recurrence
Shift the two-solution window forward
Return the current blue-disc count.
Complexity Analysis
Time: The eigenvalues of are , so . The number of iterations to exceed threshold is . Each iteration is (fixed-precision arithmetic suffices since ). Total: .
Space: .
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
int main() {
// 2b(b-1) = n(n-1) reduces to 2x^2 - y^2 = 1 (x=2b-1, y=2n-1).
// Second-order recurrence: b' = 6b - b_prev - 2, n' = 6n - n_prev - 2.
// Initial: (b0,n0)=(1,1), (b1,n1)=(3,4).
const long long T = 1000000000000LL;
long long bp = 1, np = 1;
long long bc = 3, nc = 4;
while (nc <= T) {
long long bn = 6 * bc - bp - 2;
long long nn = 6 * nc - np - 2;
bp = bc; np = nc;
bc = bn; nc = nn;
}
cout << bc << endl;
return 0;
}
"""
Project Euler Problem 100: Arranged Probability
Find the number of blue discs b in the first arrangement with n > 10^12
total discs such that P(two blue) = b(b-1)/(n(n-1)) = 1/2.
The condition 2b(b-1) = n(n-1) reduces to the negative Pell equation
2x^2 - y^2 = 1 via x = 2b-1, y = 2n-1. The second-order recurrence
b_{k+1} = 6*b_k - b_{k-1} - 2 (and same for n) generates all solutions.
Answer: 756872327473
"""
def solve():
threshold = 10**12
b_prev, n_prev = 1, 1
b_curr, n_curr = 3, 4
while n_curr <= threshold:
b_next = 6 * b_curr - b_prev - 2
n_next = 6 * n_curr - n_prev - 2
b_prev, n_prev = b_curr, n_curr
b_curr, n_curr = b_next, n_next
print(b_curr)
if __name__ == "__main__":
solve()