Project Euler Problem 71

Consider the fraction, dfrac n d, where n and d are positive integers.

Project Euler Problem 71

Solution

Answer: 428570

We seek the reduced proper fraction immediately to the left of

$$\frac37$$

among all fractions with denominator $d \le 1{,}000{,}000$.

The problem is fundamentally about Farey sequences and rational approximation.


Mathematical analysis

We want the largest reduced fraction

$$\frac nd < \frac37$$

with

$$d \le 1{,}000{,}000.$$

Equivalently, we want the fraction closest to $\frac37$ from below.


Step 1: Measure how far a fraction is from $3/7$

Consider:

$$\frac37 - \frac nd.$$

Compute:

$$\frac37 - \frac nd = \frac{3d-7n}{7d}.$$

Since $\frac nd < \frac37$,

$$3d - 7n > 0.$$

Thus minimizing the distance is equivalent to minimizing the positive integer

$$3d - 7n.$$

The smallest possible positive value is:

$$3d - 7n = 1.$$

So the optimal fraction must satisfy

$$3d - 7n = 1.$$

Rearrange:

$$7n = 3d - 1,$$

so

$$n = \frac{3d-1}{7}.$$

Therefore $3d-1$ must be divisible by $7$.


Step 2: Solve the congruence

We need:

$$3d \equiv 1 \pmod 7.$$

The inverse of $3 \pmod 7$ is $5$, because:

$$3 \cdot 5 = 15 \equiv 1 \pmod 7.$$

Hence:

$$d \equiv 5 \pmod 7.$$

We want the largest denominator $d \le 1{,}000{,}000$ satisfying this.

Compute:

$$1{,}000{,}000 \bmod 7 = 1.$$

So the largest $d \equiv 5 \pmod 7$ below one million is:

$$999{,}997.$$


Step 3: Compute the numerator

Using

$$n = \frac{3d-1}{7},$$

we get:

$$n = \frac{3(999{,}997)-1}{7} = \frac{2{,}999{,}991-1}{7} = \frac{2{,}999{,}990}{7} = 428{,}570.$$

Thus the fraction is:

$$\frac{428570}{999997}.$$

Check:

$$3(999997)-7(428570)=2999991-2999990=1,$$

so indeed it is immediately left of $\frac37$.


Python implementation

from math import gcd

LIMIT = 1_000_000

best_n = 0
best_d = 1

# For each denominator d, choose the largest n such that n/d < 3/7
for d in range(2, LIMIT + 1):

    # Largest integer strictly less than 3d/7
    n = (3 * d - 1) // 7

    # Fraction must be reduced
    if gcd(n, d) == 1:

        # Compare n/d with current best using cross multiplication
        if n * best_d > best_n * d:
            best_n = n
            best_d = d

print(best_n)

Code walkthrough

Initialization

LIMIT = 1_000_000
best_n = 0
best_d = 1

We will track the best fraction found so far.


Loop over denominators

for d in range(2, LIMIT + 1):

We examine every denominator up to one million.


Best numerator for each denominator

n = (3 * d - 1) // 7

Why this formula?

We need:

$$\frac nd < \frac37.$$

So:

$$n < \frac{3d}{7}.$$

The largest such integer is:

$$\left\lfloor \frac{3d-1}{7} \right\rfloor.$$


Ensure reduced fraction

if gcd(n, d) == 1:

Only reduced proper fractions count.


Compare fractions safely

if n * best_d > best_n * d:

Instead of floating-point division, we compare:

$$\frac nd > \frac{\text{best_n}}{\text{best_d}}$$

by cross multiplication.


Output

print(best_n)

This prints the numerator immediately left of $\frac37$.


Check against the small example

For $d \le 8$, the fraction immediately left of $\frac37$ is:

$$\frac25.$$

Indeed:

$$3 \cdot 5 - 7 \cdot 2 = 1,$$

matching our key equation.

So the method is consistent.


Final verification

Our candidate is:

$$\frac{428570}{999997}.$$

Check proximity:

$$\frac37 - \frac{428570}{999997} = \frac{1}{7 \cdot 999997},$$

which is extremely small and therefore optimal.

Everything is consistent.

Answer: 428570