All Euler problems
Project Euler

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),......

Source sync Apr 19, 2026
Problem #0492
Level Level 25
Solved By 426
Languages C++, Python
Answer 242586962923928
Length 325 words
number_theorysequencelinear_algebra

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 φ\varphi). For all n2n \geq 2, φ(n)<n\varphi(n) < n.

Proof. If n2n \geq 2, then nn has at least one prime factor pp. By the Euler product formula, φ(n)=npn(11/p)\varphi(n) = n \prod_{p \mid n}(1 - 1/p). Since at least one factor (11/p)<1(1 - 1/p) < 1, and all factors are at most 11, we get φ(n)<n\varphi(n) < n. \square

Theorem 2 (Finiteness and well-definedness of LL). For every n1n \geq 1, the totient chain n,φ(n),φ(2)(n),n, \varphi(n), \varphi^{(2)}(n), \ldots reaches 11 in finitely many steps. Specifically, L(1)=0L(1) = 0 and L(n)=1+L(φ(n))L(n) = 1 + L(\varphi(n)) for n2n \geq 2.

Proof. By Theorem 1, the sequence n>φ(n)>φ(2)(n)>n > \varphi(n) > \varphi^{(2)}(n) > \cdots is strictly decreasing for all terms 2\geq 2. Since it is a sequence of positive integers bounded below by 11, it must reach 11 in finitely many steps. The recurrence follows immediately from the definition: one step from nn to φ(n)\varphi(n), then L(φ(n))L(\varphi(n)) additional steps. \square

Lemma 1 (Logarithmic bound on chain length). For all n1n \geq 1, L(n)2log2n+1L(n) \leq 2\log_2 n + 1.

Proof. For n2n \leq 2, this is immediate (L(1)=0L(1) = 0, L(2)=1L(2) = 1). For n3n \geq 3, φ(n)\varphi(n) is even (since nn has an odd prime factor pp contributing p1p - 1 which is even, or nn is a power of 22). For even m2m \geq 2, φ(m)m/2\varphi(m) \leq m/2 (since m=2akm = 2^a \cdot k gives φ(m)m/2\varphi(m) \leq m/2). Thus after at most one step to reach an even number, each subsequent step halves the value at worst, yielding L(n)1+log2n+1L(n) \leq 1 + \log_2 n + 1. A tighter analysis gives L(n)=O(logn)L(n) = O(\log n). \square

Theorem 3 (Correctness of sieve computation of φ\varphi). The Euler product sieve correctly computes φ(n)\varphi(n) for all nNn \leq N by initializing φ[n]=n\varphi[n] = n and, for each prime pp (detected when φ[p]=p\varphi[p] = p), updating φ[kp]φ[kp](p1)/p\varphi[kp] \leftarrow \varphi[kp] \cdot (p-1)/p for all multiples kpNkp \leq N.

Proof. By the Euler product formula, φ(n)=npn(11/p)\varphi(n) = n \prod_{p \mid n}(1 - 1/p). The sieve processes each prime pp exactly once and multiplies φ[n]\varphi[n] by (11/p)(1 - 1/p) for every nn divisible by pp. After all primes pNp \leq N have been processed, φ[n]=npn(11/p)\varphi[n] = n \prod_{p \mid n}(1 - 1/p), which is the correct value. \square

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: O(NloglogN)O(N \log \log N) for the Euler product sieve (each integer nn is visited by each of its O(loglogn)O(\log \log n) distinct prime factors on average), plus O(N)O(N) for chain length computation. Total: O(NloglogN)O(N \log \log N).
  • Space: O(N)O(N) for the arrays φ[]\varphi[\cdot] and L[]L[\cdot].

Answer

242586962923928\boxed{242586962923928}

Code

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

C++ project_euler/problem_492/solution.cpp
#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;
}