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.
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
, length 2.
, length 5.
, length 7.
Length Formula
Theorem. , where is Euler’s totient function.
Proof. Each fraction in with satisfies and . For each denominator , there are exactly valid numerators with . The fractions and are endpoints. We have which accounts for . Adding gives .
Euler’s Totient Function
Definition. .
Product formula: .
Proof. By Chinese Remainder Theorem and multiplicativity: since exactly of are divisible by . For coprime : by CRT.
Asymptotic Growth
Theorem. as .
Proof. The summatory totient function satisfies . This follows from and the Euler product .
For : . Exact: 304193.
The Farey Mediant Property
Theorem. If and are consecutive in , then .
Proof. By induction. In : , so . When passing from to , a new fraction is inserted between consecutive and exactly when . The cross-differences with neighbors remain : and .
Corollary. Consecutive Farey fractions and satisfy .
Relation to the Stern-Brocot Tree
The Farey sequence of order is obtained by enumerating all fractions in the Stern-Brocot tree with denominators . The mediant insertion process builds the tree level by level.
Totient Sieve Algorithm
- Initialize for .
- For each prime : for each multiple of : .
- Sum: .
Verification Table
| | | | 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 for all .
Proof. Each prime dividing is encountered exactly once in the sieve. The update applies the factor multiplicatively. Since these factors are applied independently for each prime divisor, the final value is .
Complexity Analysis
- Totient sieve: time, space.
- Summation: single pass.
- Direct Farey enumeration: via the Stern-Brocot structure.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#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;
}
"""
Problem 918: Farey Sequence Properties
|F_n| = 1 + sum_{k=1}^{n} phi(k), where F_n is the Farey sequence of order n.
Key results:
- |F_n| ~ 3n^2/pi^2 (asymptotic)
- Mediant property: consecutive a/b, c/d satisfy bc - ad = 1
- phi(n) = n * prod_{p|n} (1 - 1/p)
Methods:
1. Totient sieve + summation
2. Mobius-based summation (cross-check)
3. Direct Farey enumeration (small n)
"""
from math import gcd
# Totient sieve
def totient_sieve(n: int) -> list:
"""Compute phi(k) for k = 0, 1, ..., n via Euler's product sieve."""
phi = list(range(n + 1))
for p in range(2, n + 1):
if phi[p] == p: # p is prime
for m in range(p, n + 1, p):
phi[m] -= phi[m] // p
return phi
def farey_length_sieve(n: int):
"""Compute |F_n| = 1 + sum phi(k) for k=1..n."""
phi = totient_sieve(n)
return 1 + sum(phi[1:])
def farey_length_brute(n: int):
"""Count fractions in F_n by enumeration."""
fracs = set()
for b in range(1, n + 1):
for a in range(0, b + 1):
if gcd(a, b) == 1:
fracs.add((a, b))
return len(fracs)
# Farey sequence generation (Stern-Brocot mediant)
def farey_sequence(n: int) -> list:
"""Generate F_n using the mediant-based algorithm."""
result = []
a, b, c, d = 0, 1, 1, n
result.append((a, b))
while c <= n:
k = (n + b) // d
a, b, c, d = c, d, k * c - a, k * d - b
result.append((a, b))
return result
# Solve
N = 1000
answer = farey_length_sieve(N)
# Verify small cases
for test_n in [1, 2, 3, 4, 5, 10]:
sieve_len = farey_length_sieve(test_n)
brute_len = farey_length_brute(test_n)
assert sieve_len == brute_len, f"n={test_n}: sieve={sieve_len}, brute={brute_len}"
# Verify mediant algorithm
for test_n in [5, 10, 20]:
seq = farey_sequence(test_n)
assert len(seq) == farey_length_sieve(test_n)
# Verify mediant property: consecutive a/b, c/d have bc - ad = 1
for i in range(len(seq) - 1):
a, b = seq[i]
c, d = seq[i + 1]
assert b * c - a * d == 1, f"Mediant property failed at {a}/{b}, {c}/{d}"
# Known: |F_1| = 2, |F_2| = 3, |F_3| = 5, |F_4| = 7
assert farey_length_sieve(1) == 2
assert farey_length_sieve(3) == 5
assert farey_length_sieve(4) == 7
print(answer)