All Euler problems
Project Euler

Ordered Fractions

By listing the set of reduced proper fractions n/d (with gcd(n,d) = 1 and 0 < n < d) for d <= 1,000,000 in ascending order, find the numerator of the fraction immediately to the left of 3/7.

Source sync Apr 19, 2026
Problem #0071
Level Level 02
Solved By 32,992
Languages C++, Python
Answer 428570
Length 341 words
modular_arithmeticnumber_theoryoptimization

Problem Statement

This archive keeps the full statement, math, and original media on the page.

Consider the fraction, $\dfrac n d$, where $n$ and $d$ are positive integers. If $n < d$ and $\operatorname{HCF}(n,d)=1$, it is called a reduced proper fraction.

If we list the set of reduced proper fractions for $d \le 8$ in ascending order of size, we get: $$\frac 1 8, \frac 1 7, \frac 1 6, \frac 1 5, \frac 1 4, \frac 2 7, \frac 1 3, \frac 3 8, \textcolor{red}{\mathbf{\frac 2 5}}, \frac 3 7, \frac 1 2, \frac 4 7, \frac 3 5, \frac 5 8, \frac 2 3, \frac 5 7, \frac 3 4, \frac 4 5, \frac 5 6, \frac 6 7, \frac 7 8$$ It can be seen that $\dfrac 2 5$ is the fraction immediately to the left of $\dfrac 3 7$.

By listing the set of reduced proper fractions for $d \le 1\,000\,000$ in ascending order of size, find the numerator of the fraction immediately to the left of $\dfrac 3 7$.

Problem 71: Ordered Fractions

Mathematical Analysis

Theorem 1 (Optimal Numerator for Fixed Denominator)

For a given denominator dd with 7d7 \nmid d, the largest integer nn satisfying n/d<3/7n/d < 3/7 is n=(3d1)/7n = \lfloor (3d - 1)/7 \rfloor. The gap between n/dn/d and 3/73/7 is:

37nd=3d7n7d,\frac{3}{7} - \frac{n}{d} = \frac{3d - 7n}{7d},

and this gap achieves its minimum value of 1/(7d)1/(7d) precisely when 3d7n=13d - 7n = 1, which holds if and only if d5(mod7)d \equiv 5 \pmod{7}.

Proof. The condition n/d<3/7n/d < 3/7 is equivalent to 7n<3d7n < 3d, i.e., n(3d1)/7n \leq \lfloor(3d-1)/7\rfloor (since 73d7 \nmid 3d when 7d7 \nmid d, the strict inequality 7n<3d7n < 3d is equivalent to 7n3d17n \leq 3d - 1, giving n(3d1)/7n \leq \lfloor(3d-1)/7\rfloor).

The gap is (3d7n)/(7d)(3d - 7n)/(7d). Since nn and dd are positive integers with 7n<3d7n < 3d, the numerator 3d7n3d - 7n is a positive integer, so 3d7n13d - 7n \geq 1.

Equality 3d7n=13d - 7n = 1 holds if and only if 3d1(mod7)3d \equiv 1 \pmod{7}. Since 315(mod7)3^{-1} \equiv 5 \pmod{7} (as 35=1513 \cdot 5 = 15 \equiv 1), this gives d5(mod7)d \equiv 5 \pmod{7}. \square

Lemma 1 (Automatic Coprimality)

When 3d7n=13d - 7n = 1, we have gcd(n,d)=1\gcd(n, d) = 1.

Proof. Let g=gcd(n,d)g = \gcd(n, d). Then g3d7n=1g \mid 3d - 7n = 1, so g=1g = 1. \square

Theorem 2 (Left Neighbor of 3/73/7 in FNF_N)

The fraction immediately to the left of 3/73/7 in the Farey sequence FNF_N (with N=106N = 10^6) is n/dn/d where dd is the largest integer N\leq N with d5(mod7)d \equiv 5 \pmod{7}, and n=(3d1)/7n = (3d-1)/7.

Proof. Among all fractions a/b<3/7a/b < 3/7 with 1bN1 \leq b \leq N, we seek to maximize a/ba/b, equivalently to minimize (3b7a)/(7b)(3b - 7a)/(7b).

By Theorem 1, for each bb the minimum gap numerator is 3b7a13b - 7a \geq 1, with equality when b5(mod7)b \equiv 5 \pmod{7}.

Claim: Among denominators achieving gap numerator 1, the fraction (3b1)/(7b)(3b-1)/(7b) is strictly increasing in bb.

Proof of claim: If b1<b2b_1 < b_2 both satisfy 3bi7ai=13b_i - 7a_i = 1, then:

a2b2a1b1=3b217b23b117b1=(3b21)b1(3b11)b27b1b2=b2b17b1b2>0.\frac{a_2}{b_2} - \frac{a_1}{b_1} = \frac{3b_2 - 1}{7b_2} - \frac{3b_1 - 1}{7b_1} = \frac{(3b_2 - 1)b_1 - (3b_1 - 1)b_2}{7b_1 b_2} = \frac{b_2 - b_1}{7b_1 b_2} > 0.

Thus we choose the largest bNb \leq N with b5(mod7)b \equiv 5 \pmod{7}.

Computation: 106mod7=110^6 \bmod 7 = 1 (since 106=142857×7+110^6 = 142857 \times 7 + 1). We need b5(mod7)b \equiv 5 \pmod{7}, so b=106(15mod7)=1063=999997b = 10^6 - (1 - 5 \bmod 7) = 10^6 - 3 = 999997. Indeed 9999975(mod7)999997 \equiv 5 \pmod{7}.

The numerator is n=(3×9999971)/7=2999990/7=428570n = (3 \times 999997 - 1)/7 = 2999990/7 = 428570.

By Lemma 1, gcd(428570,999997)=1\gcd(428570, 999997) = 1.

Dominance over gap 2\geq 2: Any fraction a/ba/b with 3b7a23b - 7a \geq 2 satisfies:

37ab27b27×106>17×999997=37428570999997\frac{3}{7} - \frac{a}{b} \geq \frac{2}{7b} \geq \frac{2}{7 \times 10^6} > \frac{1}{7 \times 999997} = \frac{3}{7} - \frac{428570}{999997}

(the middle inequality is strict since 2/106>1/9999972/10^6 > 1/999997), confirming that 428570/999997428570/999997 is the unique left neighbor. \square

Editorial

For each denominator dd, there is only one numerator worth considering: the largest integer nn with n/d<3/7n/d < 3/7, namely n=(3d1)/7n = \lfloor (3d-1)/7 \rfloor. Any smaller numerator gives a fraction farther to the left, so once dd is fixed there is no reason to check anything else.

The implementation scans all denominators up to 10610^6, skips multiples of 77 because those would hit 3/73/7 exactly, and compares the resulting candidate fraction with the current best one using cross-multiplication. This keeps the search simple while still exploiting the main observation: candidate fractions are generated directly from the inequality against 3/73/7, and only the largest candidate for each denominator survives.

Pseudocode

Start with the best fraction equal to 0/1.

For each denominator from 1 to 1,000,000:
    If the denominator is divisible by 7, skip it

    Compute the largest numerator whose fraction stays strictly below 3/7

    If this candidate fraction is larger than the current best fraction:
        replace the current best pair with this numerator and denominator

Return the numerator from the best pair.

Complexity Analysis

  • Time: O(N)O(N) with N=106N = 10^6, since we inspect each denominator once.
  • Space: O(1)O(1).

Answer

428570\boxed{428570}

Code

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

C++ project_euler/problem_071/solution.cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    // Left neighbor of 3/7 in F_{1,000,000}.
    // By Theorem 2: d = largest d <= N with d = 5 (mod 7), n = (3d-1)/7.
    // Brute-force verification scan.

    const int LIMIT = 1000000;
    long long best_n = 0, best_d = 1;

    for (int d = 1; d <= LIMIT; d++) {
        if (d % 7 == 0) continue;
        long long n = (3LL * d - 1) / 7;
        if (n * best_d > best_n * d) {
            best_n = n;
            best_d = d;
        }
    }

    cout << best_n << endl;
    return 0;
}