Totient Chains
Define the iterated Euler totient function by varphi^((0))(n) = n and varphi^((k))(n) = varphi(varphi^((k-1))(n)). The totient chain starting at n is the sequence n, varphi(n), varphi^((2))(n),......
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Define the sequence $a_1, a_2, a_3, \dots$ as: $\begin{cases} a_1 = 1 \\ a_{n+1} = 6a_n^2 + 10a_n + 3 \text{ for } n \ge 1 \end{cases}$
Examples:
$a_3 = 2359$
$a_6 = 269221280981320216750489044576319$
$a_6 \bmod 1\,000\,000\,007 = 203064689$
$a_{100} \bmod 1\,000\,000\,007 = 456482974$
Define $B(x,y,n)$ as $\sum (a_n \bmod p)$ for every prime $p$ such that $x \le p \le x+y$.
Examples:
$B(10^9, 10^3, 10^3) = 23674718882$
$B(10^9, 10^3, 10^{15}) = 20731563854$
Find $B(10^9, 10^7, 10^{15})$.
Problem 492: Totient Chains
Mathematical Foundation
Theorem 1 (Strict decrease of ). For all , .
Proof. If , then has at least one prime factor . By the Euler product formula, . Since at least one factor , and all factors are at most , we get .
Theorem 2 (Finiteness and well-definedness of ). For every , the totient chain reaches in finitely many steps. Specifically, and for .
Proof. By Theorem 1, the sequence is strictly decreasing for all terms . Since it is a sequence of positive integers bounded below by , it must reach in finitely many steps. The recurrence follows immediately from the definition: one step from to , then additional steps.
Lemma 1 (Logarithmic bound on chain length). For all , .
Proof. For , this is immediate (, ). For , is even (since has an odd prime factor contributing which is even, or is a power of ). For even , (since gives ). Thus after at most one step to reach an even number, each subsequent step halves the value at worst, yielding . A tighter analysis gives .
Theorem 3 (Correctness of sieve computation of ). The Euler product sieve correctly computes for all by initializing and, for each prime (detected when ), updating for all multiples .
Proof. By the Euler product formula, . The sieve processes each prime exactly once and multiplies by for every divisible by . After all primes have been processed, , which is the correct value.
Editorial
Iterated Euler totient: phi^(k)(n) until reaching 1. L(n) = chain length (steps to reach 1). Compute sum of L(n) for n = 2..N. We euler product sieve for phi. Finally, compute chain lengths bottom-up.
Pseudocode
Euler product sieve for phi
Compute chain lengths bottom-up
Complexity Analysis
- Time: for the Euler product sieve (each integer is visited by each of its distinct prime factors on average), plus for chain length computation. Total: .
- Space: for the arrays and .
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() {
const int LIMIT = 1000;
// Totient sieve
vector<int> phi(LIMIT + 1);
iota(phi.begin(), phi.end(), 0);
for (int p = 2; p <= LIMIT; p++) {
if (phi[p] == p) { // p is prime
for (int m = p; m <= LIMIT; m += p) {
phi[m] = phi[m] / p * (p - 1);
}
}
}
// Chain lengths
vector<int> L(LIMIT + 1, 0);
for (int n = 2; n <= LIMIT; n++) {
L[n] = 1 + L[phi[n]];
}
// Sum of chain lengths
long long total = 0;
for (int n = 2; n <= LIMIT; n++) {
total += L[n];
}
cout << "Sum of L(n) for n=2.." << LIMIT << ": " << total << endl;
// Print some chain lengths
cout << "\nChain lengths:" << endl;
for (int n = 2; n <= 20; n++) {
cout << "L(" << n << ") = " << L[n];
// Print chain
cout << " chain: " << n;
int x = n;
while (x > 1) {
x = phi[x];
cout << " -> " << x;
}
cout << endl;
}
return 0;
}
"""
Problem 492: Totient Chains
Iterated Euler totient: phi^(k)(n) until reaching 1.
L(n) = chain length (steps to reach 1).
Compute sum of L(n) for n = 2..N.
"""
def totient_sieve(limit: int) -> list:
"""Compute phi(n) for n = 0..limit."""
phi = list(range(limit + 1))
for p in range(2, limit + 1):
if phi[p] == p: # p is prime
for m in range(p, limit + 1, p):
phi[m] = phi[m] // p * (p - 1)
return phi
def chain_lengths(limit: int):
"""Compute L(n) = totient chain length for n = 0..limit.
Returns (phi, L) arrays."""
phi = totient_sieve(limit)
L = [0] * (limit + 1)
for n in range(2, limit + 1):
L[n] = 1 + L[phi[n]]
return phi, L
def totient_chain(n: int, phi: list) -> list:
"""Return the full totient chain starting at n."""
chain = [n]
while n > 1:
n = phi[n]
chain.append(n)
return chain
def solve(limit: int):
"""Sum of L(n) for n = 2..limit."""
_, L = chain_lengths(limit)
return sum(L[2:])
# Compute and display
limit = 1000
phi, L = chain_lengths(limit)
print("Totient chains for small n:")
for n in range(2, 21):
chain = totient_chain(n, phi)
print(f" n={n:2d}: chain = {' -> '.join(map(str, chain))}, L({n}) = {L[n]}")
answer_100 = sum(L[2:101])
answer_1000 = sum(L[2:1001])
print(f"\nSum of L(n) for n=2..100: {answer_100}")
print(f"Sum of L(n) for n=2..1000: {answer_1000}")