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.
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 with , the largest integer satisfying is . The gap between and is:
and this gap achieves its minimum value of precisely when , which holds if and only if .
Proof. The condition is equivalent to , i.e., (since when , the strict inequality is equivalent to , giving ).
The gap is . Since and are positive integers with , the numerator is a positive integer, so .
Equality holds if and only if . Since (as ), this gives .
Lemma 1 (Automatic Coprimality)
When , we have .
Proof. Let . Then , so .
Theorem 2 (Left Neighbor of in )
The fraction immediately to the left of in the Farey sequence (with ) is where is the largest integer with , and .
Proof. Among all fractions with , we seek to maximize , equivalently to minimize .
By Theorem 1, for each the minimum gap numerator is , with equality when .
Claim: Among denominators achieving gap numerator 1, the fraction is strictly increasing in .
Proof of claim: If both satisfy , then:
Thus we choose the largest with .
Computation: (since ). We need , so . Indeed .
The numerator is .
By Lemma 1, .
Dominance over gap : Any fraction with satisfies:
(the middle inequality is strict since ), confirming that is the unique left neighbor.
Editorial
For each denominator , there is only one numerator worth considering: the largest integer with , namely . Any smaller numerator gives a fraction farther to the left, so once is fixed there is no reason to check anything else.
The implementation scans all denominators up to , skips multiples of because those would hit 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 , 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: with , since we inspect each denominator once.
- 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() {
// 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;
}
"""
Project Euler Problem 71: Ordered Fractions
Find the numerator of the fraction immediately to the left of 3/7
in the Farey sequence F_{1,000,000}.
By Theorem 2, d is the largest integer <= N with d = 5 (mod 7),
and n = (3d - 1) / 7. Here we use a brute-force scan for verification.
"""
def solve():
LIMIT = 1_000_000
best_n, best_d = 0, 1
for d in range(1, LIMIT + 1):
if d % 7 == 0:
continue
n = (3 * d - 1) // 7
# Compare n/d > best_n/best_d via cross-multiplication
if n * best_d > best_n * d:
best_n, best_d = n, d
return best_n
if __name__ == "__main__":
answer = solve()
print(answer)