Tim Varley Logo
Tim Varley Principal AI Engineer and Tech Leader
Problem #100 medium

Arranged probability

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(BB)=(15/21)×(14/20)=1/2P(\text{BB}) = (15/21) \times (14/20) = 1/2.

The next such arrangement, for which there is exactly 50%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 1012=100000000000010^{12} = 1\,000\,000\,000\,000 discs in total, determine the number of blue discs that the box would contain.

View on Project Euler

Implementations

cpp
#include <iostream>
#include <cmath>
long long arranged_probability() {
// Use the recurrence for solutions to 2b(b-1) = n(n-1)
// Find the smallest b where n > 10^12
long long b = 15, n = 21;
while (n <= 1000000000000LL) {
long long b_new = 3 * b + 2 * n - 2;
long long n_new = 4 * b + 3 * n - 3;
b = b_new;
n = n_new;
}
return b;
}

Solution Notes

Mathematical Background

The problem requires finding integer solutions to b(b-1)/(n(n-1)) = 1/2, which simplifies to 2b(b-1) = n(n-1). This Pell-like equation has solutions generated by a recurrence relation.

Algorithm Analysis

Start from known small solution (b=15, n=21) and iterate the recurrence b_new = 3b + 2n - 2, n_new = 4b + 3n - 3 until n > 10^12.

Performance

Extremely fast (~1ms) due to simple arithmetic operations and few iterations.

Key Insights

  • Pell equation solutions follow linear recurrence
  • Direct computation without solving quadratic each time

Educational Value

Demonstrates Pell equations, recurrence relations, and efficient generation of integer solutions to Diophantine equations.

Problem #100 is taken from Project Euler and licensed under CC BY-NC-SA 4.0.