All Euler problems
Project Euler

Totient Chains

Let phi denote Euler's totient function. The totient chain of n is: n -> phi(n) -> phi(phi(n)) ->... -> 1 The length of the chain is the number of elements (including n and 1). For example, the cha...

Source sync Apr 19, 2026
Problem #0214
Level Level 06
Solved By 5,886
Languages C++, Python
Answer 1677366278943
Length 348 words
number_theorylinear_algebrasequence

Problem Statement

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

Let \(\phi \) be Euler’s totient function, i.e. for a natural number \(n\), \(\phi (n)\) is the number of \(k\), \(1 \le k \le n\), for which \(\gcd (k, n) = 1\).

By iterating \(\phi \), each positive integer generates a decreasing chain of numbers ending in \(1\).

E.g. if we start with \(5\) the sequence \(5,4,2,1\) is generated.

Here is a listing of all chains with length \(4\): \begin {align*} 5,4,2,1&\\ 7,6,2,1&\\ 8,4,2,1&\\ 9,6,2,1&\\ 10,4,2,1&\\ 12,4,2,1&\\ 14,6,2,1&\\ 18,6,2,1 \end {align*}

Only two of these chains start with a prime, their sum is \(12\).

What is the sum of all primes less than \(40000000\) which generate a chain of length \(25\)?

Problem 214: Totient Chains

Mathematical Foundation

Theorem 1 (Euler’s product formula). For n=p1a1pkakn = p_1^{a_1} \cdots p_k^{a_k} with distinct primes pip_i,

ϕ(n)=ni=1k(11pi)\phi(n) = n \prod_{i=1}^{k} \left(1 - \frac{1}{p_i}\right)

Proof. By inclusion-exclusion on the set {1,,n}\{1, \ldots, n\}, the count of integers coprime to nn is:

ϕ(n)=npinnpi+pi<pjnpipj=ni=1k(11pi)\phi(n) = n - \sum_{p_i \mid n} \frac{n}{p_i} + \sum_{p_i < p_j} \frac{n}{p_i p_j} - \cdots = n \prod_{i=1}^{k}\left(1 - \frac{1}{p_i}\right)

This follows from the Mobius inversion formula: ϕ(n)=dnμ(d)(n/d)\phi(n) = \sum_{d \mid n} \mu(d) \cdot (n/d), and the multiplicativity of μ\mu. \square

Lemma 1 (Strict decrease). For all n2n \geq 2, ϕ(n)<n\phi(n) < n. Moreover, ϕ(n)n/2\phi(n) \leq n/2 when nn is even.

Proof. For n2n \geq 2, nn has at least one prime factor pp, so ϕ(n)=n(11/pi)n(11/p)<n\phi(n) = n \prod(1 - 1/p_i) \leq n(1 - 1/p) < n. If 2n2 \mid n, then ϕ(n)n(11/2)=n/2\phi(n) \leq n(1 - 1/2) = n/2. \square

Theorem 2 (Well-definedness and computability of chain length). The totient chain of every n1n \geq 1 terminates at 1. If (n)\ell(n) denotes the chain length, then (1)=1\ell(1) = 1 and (n)=1+(ϕ(n))\ell(n) = 1 + \ell(\phi(n)) for n2n \geq 2. Moreover, (n)\ell(n) can be computed in order n=1,2,3,n = 1, 2, 3, \ldots since ϕ(n)<n\phi(n) < n.

Proof. Since ϕ(n)<n\phi(n) < n for n2n \geq 2 (Lemma 1), the sequence n,ϕ(n),ϕ2(n),n, \phi(n), \phi^2(n), \ldots is strictly decreasing until it reaches 1 (since ϕ(1)=1\phi(1) = 1). This guarantees termination. The recurrence (n)=1+(ϕ(n))\ell(n) = 1 + \ell(\phi(n)) is valid because the chain of nn is nn followed by the chain of ϕ(n)\phi(n). Since ϕ(n)<n\phi(n) < n, processing in increasing order ensures (ϕ(n))\ell(\phi(n)) is already computed when we compute (n)\ell(n). \square

Lemma 2 (Totient sieve correctness). Initialize ϕ[n]=n\phi[n] = n for all nn. For each nn from 2 to N1N-1: if ϕ[n]=n\phi[n] = n (i.e., nn is prime), then for every multiple mm of nn with m<Nm < N, set ϕ[m]ϕ[m](n1)/n\phi[m] \leftarrow \phi[m] \cdot (n-1)/n. After completion, ϕ[n]\phi[n] equals Euler’s totient for all 1n<N1 \leq n < N.

Proof. Each prime p<Np < N is identified when ϕ[p]\phi[p] still equals pp (no smaller prime has modified it, since pp is prime). The update ϕ[m]ϕ[m](p1)/p\phi[m] \leftarrow \phi[m] \cdot (p-1)/p applies the factor (11/p)(1 - 1/p) for each prime pmp \mid m. After all primes are processed, ϕ[n]=npn(11/p)=ϕ(n)\phi[n] = n \prod_{p \mid n}(1 - 1/p) = \phi(n) by Theorem 1. \square

Lemma 3 (Prime detection). After the totient sieve, nn is prime if and only if ϕ[n]=n1\phi[n] = n - 1.

Proof. If nn is prime, ϕ(n)=n1\phi(n) = n - 1. Conversely, if nn is composite with smallest prime factor pnp \leq \sqrt{n}, then ϕ(n)n(11/p)n(11/n)<n1\phi(n) \leq n(1 - 1/p) \leq n(1 - 1/\sqrt{n}) < n - 1 for n4n \geq 4. For n=1n = 1, ϕ(1)=10\phi(1) = 1 \neq 0. \square

Editorial

We totient sieve. We then chain length computation. Finally, sum primes with chain length 25. We first generate the primes required by the search, then enumerate the admissible combinations and retain only the values that satisfy the final test.

Pseudocode

Totient sieve
Chain length computation
Sum primes with chain length 25

Complexity Analysis

  • Time: Totient sieve: O(NloglogN)O(N \log \log N) (each composite is visited once per prime factor, and pNN/p=O(NloglogN)\sum_{p \leq N} N/p = O(N \log \log N) by Mertens’ theorem). Chain computation: O(N)O(N). Prime summation: O(N)O(N). Total: O(NloglogN)O(N \log \log N) where N=4×107N = 4 \times 10^7.
  • Space: O(N)O(N) for the ϕ\phi and chain arrays.

Answer

1677366278943\boxed{1677366278943}

Code

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

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

int main() {
    const int N = 40000000;

    // Sieve for Euler's totient
    vector<int> phi(N);
    iota(phi.begin(), phi.end(), 0); // phi[i] = i

    for (int i = 2; i < N; i++) {
        if (phi[i] == i) { // i is prime
            for (int j = i; j < N; j += i) {
                phi[j] = phi[j] / i * (i - 1);
            }
        }
    }

    // Compute chain lengths
    vector<int> chain(N, 0);
    chain[1] = 1;
    for (int i = 2; i < N; i++) {
        chain[i] = 1 + chain[phi[i]];
    }

    // Sum primes with chain length 25
    long long answer = 0;
    for (int i = 2; i < N; i++) {
        if (phi[i] == i - 1 && chain[i] == 25) { // i is prime and chain length is 25
            answer += i;
        }
    }

    cout << answer << endl;
    return 0;
}