All Euler problems
Project Euler

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)/(...

Source sync Apr 19, 2026
Problem #0100
Level Level 03
Solved By 18,920
Languages C++, Python
Answer 756872327473
Length 431 words
sequencelinear_algebraprobability

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 b(b1)n(n1)=12\frac{b(b-1)}{n(n-1)} = \frac{1}{2} is equivalent to the Diophantine equation

2b22bn2+n=0.(1)2b^2 - 2b - n^2 + n = 0. \tag{1}

Proof. Cross-multiplying: 2b(b1)=n(n1)2b(b-1) = n(n-1), i.e., 2b22b=n2n2b^2 - 2b = n^2 - n. \square

Theorem 2 (Reduction to Pell’s equation). Under the substitution x=2b1x = 2b - 1, y=2n1y = 2n - 1, equation (1) transforms to

2x2y2=1.(2)2x^2 - y^2 = 1. \tag{2}

Proof. From (1): 2b22b=n2n2b^2 - 2b = n^2 - n. With b=(x+1)/2b = (x+1)/2 and n=(y+1)/2n = (y+1)/2:

2(x+1)(x1)4=(y+1)(y1)4,2 \cdot \frac{(x+1)(x-1)}{4} = \frac{(y+1)(y-1)}{4}, x212=y214,\frac{x^2 - 1}{2} = \frac{y^2 - 1}{4}, 2(x21)=y21,2(x^2 - 1) = y^2 - 1, 2x2y2=1.2x^2 - y^2 = 1. \quad \square

Remark. Equation (2) is the negative Pell equation y22x2=1y^2 - 2x^2 = -1. It has solutions because 2=[1;2]\sqrt{2} = [1; \overline{2}] has an odd-length period in its continued fraction expansion.

Theorem 3 (Fundamental solution and recurrence). Equation 2x2y2=12x^2 - y^2 = 1 has fundamental solution (x0,y0)=(1,1)(x_0, y_0) = (1, 1). All positive solutions are generated by

xk+1=3xk+2yk,yk+1=4xk+3yk.x_{k+1} = 3x_k + 2y_k, \qquad y_{k+1} = 4x_k + 3y_k.

Proof. Verification that (1,1)(1,1) satisfies 211=12 \cdot 1 - 1 = 1. For the recurrence, suppose 2xk2yk2=12x_k^2 - y_k^2 = 1. Then:

2xk+12yk+12=2(3xk+2yk)2(4xk+3yk)22x_{k+1}^2 - y_{k+1}^2 = 2(3x_k + 2y_k)^2 - (4x_k + 3y_k)^2 =2(9xk2+12xkyk+4yk2)(16xk2+24xkyk+9yk2)= 2(9x_k^2 + 12x_k y_k + 4y_k^2) - (16x_k^2 + 24x_k y_k + 9y_k^2) =(1816)xk2+(2424)xkyk+(89)yk2=2xk2yk2=1.= (18 - 16)x_k^2 + (24 - 24)x_k y_k + (8 - 9)y_k^2 = 2x_k^2 - y_k^2 = 1.

The recurrence arises from multiplication in Z[2]\mathbb{Z}[\sqrt{2}]: the fundamental solution to y22x2=1y^2 - 2x^2 = -1 corresponds to the unit α=1+2\alpha = 1 + \sqrt{2}, and iterating α2k+1\alpha^{2k+1} generates all solutions to y22x2=1y^2 - 2x^2 = -1. The matrix form

(xk+1yk+1)=(3243)(xkyk)\begin{pmatrix} x_{k+1} \\ y_{k+1} \end{pmatrix} = \begin{pmatrix} 3 & 2 \\ 4 & 3 \end{pmatrix} \begin{pmatrix} x_k \\ y_k \end{pmatrix}

corresponds to squaring the fundamental unit: (1+2)2=3+22(1 + \sqrt{2})^2 = 3 + 2\sqrt{2}, giving x3x+2yx \to 3x + 2y, y4x+3yy \to 4x + 3y.

Completeness (that all solutions arise this way) follows from the theory of Pell equations: the group of units of Z[2]\mathbb{Z}[\sqrt{2}] with norm ±1\pm 1 is generated by 1+21 + \sqrt{2}. \square

Theorem 4 (Integrality of bb and nn). For each solution (xk,yk)(x_k, y_k) of (2), both xkx_k and yky_k are odd, so b=(xk+1)/2b = (x_k + 1)/2 and n=(yk+1)/2n = (y_k + 1)/2 are positive integers.

Proof. By induction. Base: x0=1x_0 = 1 and y0=1y_0 = 1 are both odd. Inductive step: if xkx_k and yky_k are odd, then xk+1=3xk+2yk=odd+even=oddx_{k+1} = 3x_k + 2y_k = \text{odd} + \text{even} = \text{odd}, and yk+1=4xk+3yk=even+odd=oddy_{k+1} = 4x_k + 3y_k = \text{even} + \text{odd} = \text{odd}. \square

Theorem 5 (Second-order recurrence for bb and nn). The sequences bkb_k and nkn_k satisfy the second-order linear recurrence

bk+1=6bkbk12,nk+1=6nknk12,b_{k+1} = 6b_k - b_{k-1} - 2, \qquad n_{k+1} = 6n_k - n_{k-1} - 2,

with initial values (b0,n0)=(1,1)(b_0, n_0) = (1, 1) and (b1,n1)=(3,4)(b_1, n_1) = (3, 4).

Proof. From the matrix recurrence in Theorem 3, composing two steps of the (x,y)(x, y) recurrence and substituting b=(x+1)/2b = (x+1)/2, n=(y+1)/2n = (y+1)/2:

The characteristic equation of the matrix M=(3243)M = \begin{pmatrix} 3 & 2 \\ 4 & 3 \end{pmatrix} is λ26λ+1=0\lambda^2 - 6\lambda + 1 = 0 (trace =6= 6, determinant =1= 1). Therefore xk+1=6xkxk1x_{k+1} = 6x_k - x_{k-1} and yk+1=6ykyk1y_{k+1} = 6y_k - y_{k-1}. Substituting xk=2bk1x_k = 2b_k - 1:

2bk+11=6(2bk1)(2bk11)=12bk2bk14,2b_{k+1} - 1 = 6(2b_k - 1) - (2b_{k-1} - 1) = 12b_k - 2b_{k-1} - 4, bk+1=6bkbk12.b_{k+1} = 6b_k - b_{k-1} - 2.

The same derivation applies to nkn_k. Initial values: (x0,y0)=(1,1)(x_0, y_0) = (1, 1) gives (b0,n0)=(1,1)(b_0, n_0) = (1, 1); (x1,y1)=(5,7)(x_1, y_1) = (5, 7) gives (b1,n1)=(3,4)(b_1, n_1) = (3, 4). Verification: 232=12=432 \cdot 3 \cdot 2 = 12 = 4 \cdot 3. \square

Verification.

kkbkb_knkn_kCheck: 2b(b1)=n(n1)2b(b-1) = n(n-1)
0110=00 = 0
13412=1212 = 12
21521420=420420 = 420
38512014280=1428014280 = 14280

Editorial

The probability condition is awkward in (b,n)(b,n) 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 (b,n)(b,n) 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 101210^{12}. 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 MM are 3±223 \pm 2\sqrt{2}, so nk(3+22)kn_k \sim (3 + 2\sqrt{2})^k. The number of iterations to exceed threshold TT is log3+22T=O(logT)\lceil \log_{3+2\sqrt{2}} T \rceil = O(\log T). Each iteration is O(1)O(1) (fixed-precision arithmetic suffices since T=1012<240T = 10^{12} < 2^{40}). Total: O(logT)O(\log T).

Space: O(1)O(1).

Answer

756872327473\boxed{756872327473}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_100/solution.cpp
#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;
}