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 both n and 1. For example, the...

Source sync May 21, 2026
Problem #0214
Level Level 06
Solved By 5,886
Languages C++, Python
Answer 1677366278943
Length 338 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 Development

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

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

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

ϕ(n)=n−∑pi∣nnpi+∑pi<pjnpipj−⋯=n∏i=1k(1−1pi).□\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). \qquad \square

Lemma 1 (Strict decrease). For all n≥2n \geq 2, ϕ(n)<n\phi(n) < n.

Proof. If n≥2n \geq 2, then nn has at least one prime factor pp, so

ϕ(n)=n∏q∣n(1−1q)≤n(1−1p)<n.\phi(n) = n \prod_{q \mid n} \left(1 - \frac{1}{q}\right) \leq n\left(1 - \frac{1}{p}\right) < n.

□\square

Theorem 2 (Chain recurrence). If ℓ(n)\ell(n) denotes the totient-chain length, then ℓ(1)=1\ell(1) = 1 and

ℓ(n)=1+ℓ(ϕ(n))\ell(n) = 1 + \ell(\phi(n))

for all n≥2n \geq 2.

Proof. The chain of nn is nn followed by the chain of ϕ(n)\phi(n). Since ϕ(n)<n\phi(n) < n by Lemma 1, repeated iteration must eventually reach 1. □\square

Lemma 2 (Totient sieve correctness). Initialize ϕ[n]=n\phi[n] = n for all nn. For each prime pp, updating every multiple mm by

ϕ[m]←ϕ[m]⋅p−1p\phi[m] \leftarrow \phi[m] \cdot \frac{p-1}{p}

produces the correct totient values for all mm.

Proof. Each prime divisor pp of mm contributes exactly one factor (1−1/p)(1 - 1/p) in Euler’s product formula, and the sieve applies those factors once per prime divisor. □\square

Lemma 3 (Prime detection). For n≥2n \geq 2, we have ϕ(n)=n−1\phi(n) = n - 1 if and only if nn is prime.

Proof. If nn is prime, then every positive integer below it is coprime to nn, so ϕ(n)=n−1\phi(n) = n - 1. Conversely, a composite nn has a nontrivial prime factor pp, so at least the multiples of pp below nn are excluded, forcing ϕ(n)<n−1\phi(n) < n - 1. □\square

Editorial

The recursion for the chain length is the important structural fact: once ϕ(n)\phi(n) is known, the chain length of nn is just one more than the chain length of a smaller number. That means a sieve order is perfect for this problem, because when we reach nn, both ϕ(n)\phi(n) and ℓ(ϕ(n))\ell(\phi(n)) are already available.

So we combine three tasks in one pass. First compute the totient values with the standard sieve. Then, in increasing order, use

ℓ(n)=1+ℓ(ϕ(n))\ell(n) = 1 + \ell(\phi(n))

to fill the chain-length array. Whenever nn is prime, identified by ϕ(n)=n−1\phi(n) = n-1, check whether the chain length is 25 and add it if it is.

Pseudocode

Set LIMIT = 40,000,000.
Initialize phi[n] = n for 0 <= n < LIMIT.
Initialize chain[1] = 1 and chain[n] = 0 otherwise.
answer = 0

For n from 2 to LIMIT - 1:
    If phi[n] = n:
        For each multiple m of n:
            phi[m] -= phi[m] / n

    chain[n] = chain[phi[n]] + 1

    If phi[n] = n - 1 and chain[n] = 25:
        answer += n

Return answer

Complexity Analysis

  • Time: O(Nlog⁡log⁡N)O(N \log \log N) for the totient sieve, plus O(N)O(N) for the chain updates and prime filtering.
  • Space: O(N)O(N) for the totient table and the chain-length table.

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;
}