All Euler problems
Project Euler

Totient Sum Optimization

Find sum_(k=1)^N varphi(k) (mod 10^9+7) for N = 10^7.

Source sync Apr 19, 2026
Problem #0927
Level Level 36
Solved By 195
Languages C++, Python
Answer 207282955
Length 230 words
number_theorymodular_arithmeticanalytic_math

Problem Statement

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

A full $k$-ary tree is a tree with a single root node, such that every node is either a leaf or has exactly $k$ ordered children. The height of a $k$-ary tree is the number of edges in the longest path from the root to a leaf.

For instance, there is one full 3-ary tree of height 0, one full 3-ary tree of height 1, and seven full 3-ary trees of height 2. These seven are shown below.

Problem illustration

For integers $n$ and $k$ with $n\ge 0$ and $k \ge 2$, define $t_k(n)$ to be the number of full $k$-ary trees of height $n$ or less.

Thus, $t_3(0) = 1$, $t_3(1) = 2$, and $t_3(2) = 9$. Also, $t_2(0) = 1$, $t_2(1) = 2$, and $t_2(2) = 5$.

Define $S_k$ to be the set of positive integers $m$ such that $m$ divides $t_k(n)$ for some integer $n\ge 0$. For instance, the above values show that 1, 2, and 5 are in $S_2$ and 1, 2, 3, and 9 are in $S_3$.

Let $S = \bigcap_p S_p$ where the intersection is taken over all primes $p$. Finally, define $R(N)$ to be the sum of all elements of $S$ not exceeding $N$. You are given that $R(20) = 18$ and $R(1000) = 2089$.

Find $R(10^7)$.

Problem 927: Totient Sum Optimization

Mathematical Foundation

Theorem 1 (Euler’s Product Formula). For n=p1a1prarn = p_1^{a_1} \cdots p_r^{a_r}:

φ(n)=npn(11p)=i=1rpiai1(pi1).\varphi(n) = n \prod_{p \mid n}\left(1 - \frac{1}{p}\right) = \prod_{i=1}^{r} p_i^{a_i - 1}(p_i - 1).

Proof. By inclusion-exclusion on the set {1,,n}\{1, \ldots, n\}, subtracting multiples of each prime pip_i dividing nn:

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

Alternatively, since φ\varphi is multiplicative and φ(pa)=pa1(p1)\varphi(p^a) = p^{a-1}(p-1) (there are papa1p^a - p^{a-1} integers in {1,,pa}\{1,\ldots,p^a\} coprime to pp), the product formula follows from multiplicativity. \square

Theorem 2 (Divisor Sum Identity). For all n1n \geq 1: dnφ(d)=n\displaystyle\sum_{d \mid n} \varphi(d) = n.

Proof. Partition {1,2,,n}\{1, 2, \ldots, n\} by the value of gcd(k,n)\gcd(k, n). For each divisor dnd \mid n, the set {k{1,,n}:gcd(k,n)=d}\{k \in \{1,\ldots,n\} : \gcd(k,n) = d\} has cardinality φ(n/d)\varphi(n/d) (since gcd(k,n)=d\gcd(k,n) = d iff gcd(k/d,n/d)=1\gcd(k/d, n/d) = 1, and k/dk/d ranges over {1,,n/d}\{1,\ldots,n/d\}). Summing: dnφ(n/d)=n\sum_{d \mid n} \varphi(n/d) = n. Reindexing d=n/dd' = n/d gives dnφ(d)=n\sum_{d' \mid n} \varphi(d') = n. \square

Theorem 3 (Sub-linear Recursion for Φ(N)\Phi(N)). Let Φ(N)=k=1Nφ(k)\Phi(N) = \sum_{k=1}^{N} \varphi(k). Then:

Φ(N)=N(N+1)2k=2NΦ ⁣(Nk).\Phi(N) = \frac{N(N+1)}{2} - \sum_{k=2}^{N} \Phi\!\left(\left\lfloor \frac{N}{k} \right\rfloor\right).

Proof. From Theorem 2, summing over n=1,,Nn = 1, \ldots, N:

n=1Nn=n=1Ndnφ(d)=d=1Nφ(d)Nd.\sum_{n=1}^{N} n = \sum_{n=1}^{N} \sum_{d \mid n} \varphi(d) = \sum_{d=1}^{N} \varphi(d) \left\lfloor \frac{N}{d} \right\rfloor.

The left side is N(N+1)/2N(N+1)/2. Grouping by m=N/dm = \lfloor N/d \rfloor:

N(N+1)2=d=1Nφ(d)Nd.\frac{N(N+1)}{2} = \sum_{d=1}^{N} \varphi(d) \left\lfloor \frac{N}{d} \right\rfloor.

Equivalently, using hyperbola-method notation: d=1Nφ(d)N/d=m=1NΦ(N/m)\sum_{d=1}^{N} \varphi(d) \lfloor N/d \rfloor = \sum_{m=1}^{N} \Phi(\lfloor N/m \rfloor) (by Dirichlet’s hyperbola method applied to φ1=id\varphi * \mathbf{1} = \mathrm{id}). Isolating m=1m = 1:

Φ(N)=N(N+1)2m=2NΦ ⁣(Nm).\Phi(N) = \frac{N(N+1)}{2} - \sum_{m=2}^{N} \Phi\!\left(\left\lfloor\frac{N}{m}\right\rfloor\right). \quad \square

Lemma 1 (Complexity of the Recursion). Using memoization and grouping identical values of N/k\lfloor N/k \rfloor, the recursion computes Φ(N)\Phi(N) in O(N2/3)O(N^{2/3}) time and O(N2/3)O(N^{2/3}) space.

Proof. The set {N/k:1kN}\{\lfloor N/k \rfloor : 1 \leq k \leq N\} has O(N)O(\sqrt{N}) distinct values. With a sieve precomputing Φ(m)\Phi(m) for mN2/3m \leq N^{2/3}, the remaining O(N1/3)O(N^{1/3}) values are handled by the recursion, each requiring O(m)O(\sqrt{m}) work. Total: O(N2/3)O(N^{2/3}). \square

Theorem 4 (Asymptotic Formula). Φ(N)=3N2π2+O(NlogN)\Phi(N) = \frac{3N^2}{\pi^2} + O(N \log N).

Proof. Since φ=μid\varphi = \mu * \mathrm{id} (Mobius inversion of Theorem 2), the Dirichlet series φ(n)/ns=ζ(s1)/ζ(s)\sum \varphi(n)/n^s = \zeta(s-1)/\zeta(s). By standard Tauberian arguments (or Perron’s formula), Φ(N)=N22ζ(2)+O(NlogN)=3N2π2+O(NlogN)\Phi(N) = \frac{N^2}{2\zeta(2)} + O(N \log N) = \frac{3N^2}{\pi^2} + O(N \log N), using ζ(2)=π2/6\zeta(2) = \pi^2/6. \square

Editorial

Compute the sum of Euler’s totient function phi(k) for k = 1..N, modulo 10^9+7. Uses a sieve-based approach to compute all phi values efficiently. Key ideas:.

Pseudocode

    Sieve phi(n) for n = 1..N, then sum
    phi[1..N]
    For n from 1 to N:
        phi[n] = n

    For p from 2 to N:
        if phi[p] == p: // p is prime
            For m from p to N step p:
                phi[m] = phi[m] / p * (p - 1)

    S = 0
    For n from 1 to N:
        S = (S + phi[n]) mod MOD

    Return S

Complexity Analysis

  • Time: O(NloglogN)O(N \log \log N) for the Euler sieve. The inner loop over primes pp processes N/p\lfloor N/p \rfloor multiples, and pNN/p=O(NloglogN)\sum_{p \leq N} N/p = O(N \log \log N) by Mertens’ theorem.
  • Space: O(N)O(N) for the array φ[1..N]\varphi[1..N].

For the sub-linear approach: O(N2/3)O(N^{2/3}) time and space (see Lemma 1).

Answer

207282955\boxed{207282955}

Code

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

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

/*
 * Problem 927: Totient Sum Optimization
 *
 * Find sum_{k=1}^{10^7} phi(k) mod 10^9+7.
 *
 * Euler sieve: phi(n) = n * prod_{p|n} (1 - 1/p).
 * Initialize phi[n] = n, then for each prime p:
 *   phi[m] -= phi[m]/p for all m divisible by p.
 *
 * Key identity: sum_{d|n} phi(d) = n.
 * Asymptotic: Phi(N) = 3N^2/pi^2 + O(N log N).
 *
 * Complexity: O(N log log N) sieve, O(N) summation.
 */

int main() {
    const int N = 10000000;
    const long long MOD = 1000000007;
    vector<int> phi(N + 1);
    iota(phi.begin(), phi.end(), 0);
    for (int p = 2; p <= N; p++) {
        if (phi[p] == p) { // p is prime
            for (int m = p; m <= N; m += p) {
                phi[m] -= phi[m] / p;
            }
        }
    }
    long long ans = 0;
    for (int k = 1; k <= N; k++) {
        ans = (ans + phi[k]) % MOD;
    }
    cout << ans << endl;

    // Verify: phi(1)=1, phi(2)=1, phi(6)=2, phi(12)=4
    assert(phi[1] == 1);
    assert(phi[2] == 1);
    assert(phi[6] == 2);
    assert(phi[12] == 4);

    return 0;
}