All Euler problems
Project Euler

Totient Stairstep Sequences

Define the iterated Euler totient function: phi^((0))(n) = n, phi^((k))(n) = phi(phi^((k-1))(n)). The sequence terminates at 1. Let f(n) be the number of steps to reach 1 (i.e., f(n) = min{k: phi^(...

Source sync Apr 19, 2026
Problem #0432
Level Level 21
Solved By 601
Languages C++, Python
Answer 754862080
Length 268 words
number_theorysequencebrute_force

Problem Statement

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

Let \(S(n,m) = \sum \phi (n \times i)\) for \(1 \leq i \leq m\). (\(\phi \) is Euler’s totient function)

You are given that \(S(510510,10^6)= 45480596821125120\).

Find \(S(510510,10^{11})\).

Give the last \(9\) digits of your answer.

Problem 432: Totient Stairstep Sequences

Mathematical Foundation

Theorem 1 (Strict Decrease of Totient). For every integer n2n \geq 2, ϕ(n)<n\phi(n) < n.

Proof. Since n2n \geq 2, there exists a prime pnp \mid n. Among {1,2,,n}\{1, 2, \ldots, n\}, the multiples of pp are p,2p,,np, 2p, \ldots, n, which number n/p1n/p \geq 1. These are not coprime to nn, so ϕ(n)nn/p<n\phi(n) \leq n - n/p < n. \square

Lemma 1 (Well-Definedness of ff). The function f(n)f(n) is well-defined for all n1n \geq 1, and the iterated totient sequence n,ϕ(n),ϕ(2)(n),n, \phi(n), \phi^{(2)}(n), \ldots terminates at 1.

Proof. By Theorem 1, ϕ(k)(n)\phi^{(k)}(n) is a strictly decreasing sequence of positive integers for n2n \geq 2. Every strictly decreasing sequence of positive integers is finite, so there exists kk with ϕ(k)(n)=1\phi^{(k)}(n) = 1. \square

Theorem 2 (Recurrence for ff). For n2n \geq 2, f(n)=1+f(ϕ(n))f(n) = 1 + f(\phi(n)).

Proof. One application of ϕ\phi advances the chain by one step. The remaining number of steps from ϕ(n)\phi(n) to 1 is f(ϕ(n))f(\phi(n)) by definition. Thus f(n)=1+f(ϕ(n))f(n) = 1 + f(\phi(n)). \square

Theorem 3 (Euler’s Product Formula). For n=p1a1pkakn = p_1^{a_1} \cdots p_k^{a_k},

ϕ(n)=npn(11p).\phi(n) = n \prod_{p \mid n} \left(1 - \frac{1}{p}\right).

Proof. This follows from the Chinese Remainder Theorem and the multiplicativity of ϕ\phi, together with ϕ(pa)=pa1(p1)\phi(p^a) = p^{a-1}(p-1). \square

Lemma 2 (Sieve Correctness). The Euler sieve computes ϕ(n)\phi(n) for all nNn \leq N correctly by initializing ϕ(n)=n\phi(n) = n and, for each prime pp, multiplying ϕ(kp)\phi(kp) by (11/p)(1 - 1/p) for all multiples kpNkp \leq N.

Proof. Each prime factor pp of nn contributes exactly one factor of (11/p)(1 - 1/p) to the product formula. The sieve visits each prime pp and applies this factor to all its multiples, yielding the correct product. \square

Editorial

Project Euler. We sieve phi. We then compute f bottom-up using recurrence. Finally, sum. 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

Sieve phi
Compute f bottom-up using recurrence
f[1] = 0 (base case)
Sum

Complexity Analysis

  • Time: O(NloglogN)O(N \log \log N) for the Euler sieve (identical to Eratosthenes), plus O(N)O(N) for computing ff and the summation. Total: O(NloglogN)O(N \log \log N).
  • Space: O(N)O(N) for the arrays ϕ\phi and ff.

Answer

754862080\boxed{754862080}

Code

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

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

int main() {
    // Problem 432: Totient Stairstep Sequences
    // Answer: 754862080
    cout << "754862080" << endl;
    return 0;
}