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 May 21, 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 n≥2n \geq 2, φ(n)<n\varphi(n) < n.

Proof. If n≥2n \geq 2, then nn has at least one prime factor pp. By the Euler product formula, φ(n)=n∏p∣n(1−1/p)\varphi(n) = n \prod_{p \mid n}(1 - 1/p). Since at least one factor (1−1/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 n≥1n \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 n≥2n \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 n≥1n \geq 1, L(n)≤2log⁡2n+1L(n) \leq 2\log_2 n + 1.

Proof. For n≤2n \leq 2, this is immediate (L(1)=0L(1) = 0, L(2)=1L(2) = 1). For n≥3n \geq 3, φ(n)\varphi(n) is even (since nn has an odd prime factor pp contributing p−1p - 1 which is even, or nn is a power of 22). For even m≥2m \geq 2, φ(m)≤m/2\varphi(m) \leq m/2 (since m=2a⋅km = 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+log⁡2n+1L(n) \leq 1 + \log_2 n + 1. A tighter analysis gives L(n)=O(log⁡n)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 n≤Nn \leq N by initializing φ[n]=n\varphi[n] = n and, for each prime pp (detected when φ[p]=p\varphi[p] = p), updating φ[kp]←φ[kp]⋅(p−1)/p\varphi[kp] \leftarrow \varphi[kp] \cdot (p-1)/p for all multiples kp≤Nkp \leq N.

Proof. By the Euler product formula, φ(n)=n∏p∣n(1−1/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 (1−1/p)(1 - 1/p) for every nn divisible by pp. After all primes p≤Np \leq N have been processed, φ[n]=n∏p∣n(1−1/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(Nlog⁡log⁡N)O(N \log \log N) for the Euler product sieve (each integer nn is visited by each of its O(log⁡log⁡n)O(\log \log n) distinct prime factors on average), plus O(N)O(N) for chain length computation. Total: O(Nlog⁡log⁡N)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;
}