All Euler problems
Project Euler

Farey Sequence Properties

The Farey sequence F_n consists of all reduced fractions a/b with 0 <= a <= b <= n, arranged in ascending order. Find |F_1000|, the number of terms in F_1000.

Source sync Apr 19, 2026
Problem #0918
Level Level 13
Solved By 1,363
Languages C++, Python
Answer 304193
Length 309 words
number_theorysequenceanalytic_math

Problem Statement

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

The sequence \(a_n\) is defined by \(a_1=1\), and then recursively for \(n\geq 1\): \begin {align*} a_{2n} &= 2a_n\\ a_{2n+1} &= a_n-3a_{n+1} \end {align*}

The first ten terms are \(1, 2, -5, 4, 17, -10, -17, 8, -47, 34\).

Define \(\displaystyle S(N) = \sum _{n=1}^N a_n\). You are given \(S(10) = -13\).

Find \(S(10^{12})\).

Problem 918: Farey Sequence Properties

Mathematical Analysis

Definition and Examples

F1={0/1,1/1}F_1 = \{0/1, 1/1\}, length 2.

F3={0/1,1/3,1/2,2/3,1/1}F_3 = \{0/1, 1/3, 1/2, 2/3, 1/1\}, length 5.

F4={0/1,1/4,1/3,1/2,2/3,3/4,1/1}F_4 = \{0/1, 1/4, 1/3, 1/2, 2/3, 3/4, 1/1\}, length 7.

Length Formula

Theorem. Fn=1+k=1nφ(k)|F_n| = 1 + \sum_{k=1}^{n} \varphi(k), where φ\varphi is Euler’s totient function.

Proof. Each fraction a/ba/b in FnF_n with 0<a<b0 < a < b satisfies gcd(a,b)=1\gcd(a, b) = 1 and 1bn1 \le b \le n. For each denominator bb, there are exactly φ(b)\varphi(b) valid numerators a{1,,b1}a \in \{1, \ldots, b-1\} with gcd(a,b)=1\gcd(a,b) = 1. The fractions 0/10/1 and b/b=1/1b/b = 1/1 are endpoints. We have φ(1)=1\varphi(1) = 1 which accounts for 1/11/1. Adding 0/10/1 gives Fn=1+k=1nφ(k)|F_n| = 1 + \sum_{k=1}^{n}\varphi(k). \square

Euler’s Totient Function

Definition. φ(n)={k:1kn,gcd(k,n)=1}\varphi(n) = |\{k : 1 \le k \le n, \gcd(k, n) = 1\}|.

Product formula: φ(n)=npn(11p)\varphi(n) = n \prod_{p \mid n} \left(1 - \frac{1}{p}\right).

Proof. By Chinese Remainder Theorem and multiplicativity: φ(pa)=pa1(p1)\varphi(p^a) = p^{a-1}(p-1) since exactly pa1p^{a-1} of {1,,pa}\{1, \ldots, p^a\} are divisible by pp. For coprime m,nm, n: φ(mn)=φ(m)φ(n)\varphi(mn) = \varphi(m)\varphi(n) by CRT. \square

Asymptotic Growth

Theorem. Fn3n2π2|F_n| \sim \frac{3n^2}{\pi^2} as nn \to \infty.

Proof. The summatory totient function satisfies k=1nφ(k)=n22ζ(2)+O(nlogn)=3n2π2+O(nlogn)\sum_{k=1}^{n} \varphi(k) = \frac{n^2}{2\zeta(2)} + O(n \log n) = \frac{3n^2}{\pi^2} + O(n \log n). This follows from k=1nφ(k)=12d=1nμ(d)n/d(n/d+1)\sum_{k=1}^{n} \varphi(k) = \frac{1}{2}\sum_{d=1}^{n} \mu(d)\lfloor n/d \rfloor(\lfloor n/d \rfloor + 1) and the Euler product ζ(2)=π2/6\zeta(2) = \pi^2/6. \square

For n=1000n = 1000: 3×106/π23039643 \times 10^6/\pi^2 \approx 303964. Exact: 304193.

The Farey Mediant Property

Theorem. If a/ba/b and c/dc/d are consecutive in FnF_n, then bcad=1bc - ad = 1.

Proof. By induction. In F1F_1: 1011=11 \cdot 0 - 1 \cdot 1 = -1, so bcad=1|bc - ad| = 1. When passing from Fn1F_{n-1} to FnF_n, a new fraction (a+c)/(b+d)(a+c)/(b+d) is inserted between consecutive a/ba/b and c/dc/d exactly when b+d=nb + d = n. The cross-differences with neighbors remain ±1\pm 1: (a+c)ba(b+d)=cbad=1(a+c)b - a(b+d) = cb - ad = 1 and c(b+d)(a+c)d=cbad=1c(b+d) - (a+c)d = cb - ad = 1. \square

Corollary. Consecutive Farey fractions a/ba/b and c/dc/d satisfy c/da/b=1/(bd)c/d - a/b = 1/(bd).

Relation to the Stern-Brocot Tree

The Farey sequence of order nn is obtained by enumerating all fractions in the Stern-Brocot tree with denominators n\le n. The mediant insertion process builds the tree level by level.

Totient Sieve Algorithm

  1. Initialize φ[k]=k\varphi[k] = k for k=0,1,,nk = 0, 1, \ldots, n.
  2. For each prime pp: for each multiple mm of pp: φ[m]=φ[m]/p\varphi[m] \mathrel{-}= \varphi[m] / p.
  3. Sum: Fn=1+k=1nφ[k]|F_n| = 1 + \sum_{k=1}^{n} \varphi[k].

Verification Table

| nn | Fn|F_n| | 3n2/π23n^2/\pi^2 | Ratio | |-----|--------|-------------|-------| | 10 | 33 | 30.4 | 1.086 | | 100 | 3045 | 3040 | 1.002 | | 500 | 76117 | 75991 | 1.002 | | 1000 | 304193 | 303964 | 1.001 |

Proof of Correctness

Theorem. The sieve correctly computes φ(k)\varphi(k) for all knk \le n.

Proof. Each prime pp dividing kk is encountered exactly once in the sieve. The update φ[k]φ[k]φ[k]/p\varphi[k] \leftarrow \varphi[k] - \varphi[k]/p applies the factor (11/p)(1 - 1/p) multiplicatively. Since these factors are applied independently for each prime divisor, the final value is kpk(11/p)=φ(k)k \prod_{p \mid k}(1 - 1/p) = \varphi(k). \square

Complexity Analysis

  • Totient sieve: O(nloglogn)O(n \log \log n) time, O(n)O(n) space.
  • Summation: O(n)O(n) single pass.
  • Direct Farey enumeration: O(n2/logn)O(n^2 / \log n) via the Stern-Brocot structure.

Answer

304193\boxed{304193}

Code

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

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

/*
 * Problem 918: Farey Sequence Properties
 *
 * |F_n| = 1 + sum_{k=1}^{n} phi(k)
 *
 * Two methods:
 *   1. Totient sieve: compute phi(k) for k=1..n, then sum
 *   2. Mediant-based enumeration (verification)
 *
 * Asymptotic: |F_n| ~ 3n^2/pi^2
 * Mediant property: consecutive a/b, c/d satisfy bc - ad = 1
 */

const int N = 1000;

/* Euler's totient sieve */
vector<int> totient_sieve(int n) {
    vector<int> phi(n + 1);
    iota(phi.begin(), phi.end(), 0);
    for (int p = 2; p <= n; p++) {
        if (phi[p] == p) {  // p is prime
            for (int m = p; m <= n; m += p) {
                phi[m] -= phi[m] / p;
            }
        }
    }
    return phi;
}

/* Mediant-based Farey sequence generation */
long long farey_count_mediant(int n) {
    long long count = 0;
    int a = 0, b = 1, c = 1, d = n;
    count++;  // 0/1
    while (c <= n) {
        int k = (n + b) / d;
        int na = c, nb = d, nc = k * c - a, nd = k * d - b;
        a = na; b = nb; c = nc; d = nd;
        count++;
    }
    return count;
}

int main() {
    auto phi = totient_sieve(N);

    // Method 1: Sieve
    long long ans = 1;
    for (int k = 1; k <= N; k++)
        ans += phi[k];

    // Method 2: Mediant enumeration
    long long ans2 = farey_count_mediant(N);
    assert(ans == ans2);

    // Verify small cases
    assert(1 + phi[1] == 2);  // |F_1| = 2
    long long f3 = 1;
    for (int k = 1; k <= 3; k++) f3 += phi[k];
    assert(f3 == 5);  // |F_3| = 5

    cout << ans << endl;
    return 0;
}