All Euler problems
Project Euler

Sum of Divisors of Sum of Divisors

Let sigma(n) = sum_(d | n) d denote the sum-of-divisors function. Define S(N) = sum_(n=1)^N sigma(sigma(n)). Compute S(N) for large N.

Source sync Apr 19, 2026
Problem #0632
Level Level 21
Solved By 601
Languages C++, Python
Answer 728378714
Length 208 words
number_theorymodular_arithmeticanalytic_math

Problem Statement

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

For an integer \(n\), we define the square prime factors of \(n\) to be the primes whose square divides \(n\). For example, the square prime factors of \(1500=2^2 \times 3 \times 5^3\) are \(2\) and \(5\).

Let \(C_k(N)\) be the number of integers between \(1\) and \(N\) inclusive with exactly \(k\) square prime factors. You are given some values of \(C_k(N)\) in the table below.

\[ \begin {array}{|c|c|c|c|c|c|c|} \hline & k = 0 & k = 1 & k = 2 & k = 3 & k = 4 & k = 5 \\ \hline N=10 & 7 & 3 & 0 & 0 & 0 & 0 \\ \hline N=10^2 & 61 & 36 & 3 & 0 & 0 & 0 \\ \hline N=10^3 & 608 & 343 & 48 & 1 & 0 & 0 \\ \hline N=10^4 & 6083 & 3363 & 533 & 21 & 0 & 0 \\ \hline N=10^5 & 60794 & 33562 & 5345 & 297 & 2 & 0 \\ \hline N=10^6 & 607926 & 335438 & 53358 & 3218 & 60 & 0 \\ \hline N=10^7 & 6079291 & 3353956 & 533140 & 32777 & 834 & 2 \\ \hline N=10^8 & 60792694 & 33539196 & 5329747 & 329028 & 9257 & 78 \\ \hline \end {array} \]

Find the product of all non-zero \(C_k(10^{16})\). Give the result reduced modulo \(1\,000\,000\,007\).

Problem 632: Sum of Divisors of Sum of Divisors

Mathematical Foundation

Theorem 1 (Multiplicativity of σ\sigma). The function σ\sigma is multiplicative: σ(mn)=σ(m)σ(n)\sigma(mn) = \sigma(m)\sigma(n) whenever gcd(m,n)=1\gcd(m,n) = 1.

Proof. If gcd(m,n)=1\gcd(m,n) = 1, then every divisor dd of mnmn factors uniquely as d=d1d2d = d_1 d_2 with d1md_1 \mid m and d2nd_2 \mid n. Therefore

σ(mn)=dmnd=d1md2nd1d2=(d1md1)(d2nd2)=σ(m)σ(n).\sigma(mn) = \sum_{d \mid mn} d = \sum_{d_1 \mid m} \sum_{d_2 \mid n} d_1 d_2 = \left(\sum_{d_1 \mid m} d_1\right)\left(\sum_{d_2 \mid n} d_2\right) = \sigma(m)\sigma(n). \quad \square

Lemma 1 (Prime Power Formula). For a prime pp and integer k1k \geq 1,

σ(pk)=pk+11p1.\sigma(p^k) = \frac{p^{k+1} - 1}{p - 1}.

Proof. The divisors of pkp^k are 1,p,p2,,pk1, p, p^2, \ldots, p^k. Their sum is the geometric series i=0kpi=(pk+11)/(p1)\sum_{i=0}^{k} p^i = (p^{k+1}-1)/(p-1). \square

Theorem 2 (Divisor Sieve Correctness). The additive sieve that, for each d=1,,Nd = 1, \ldots, N, adds dd to σ(m)\sigma(m) for every multiple m=d,2d,,N/ddm = d, 2d, \ldots, \lfloor N/d \rfloor \cdot d, correctly computes σ(n)\sigma(n) for all nNn \leq N.

Proof. For a fixed nNn \leq N, the sieve adds dd to σ(n)\sigma(n) exactly when dnd \mid n and dNd \leq N. Since nNn \leq N, every divisor dd of nn satisfies dnNd \leq n \leq N, so all divisors are counted. Hence σ(n)=dnd\sigma(n) = \sum_{d \mid n} d is computed correctly. \square

Theorem 3 (Average Order). n=1Nσ(n)=π212N2+O(NlogN)\sum_{n=1}^{N} \sigma(n) = \frac{\pi^2}{12} N^2 + O(N \log N).

Proof. n=1Nσ(n)=d=1NdN/d=d=1Nd(N/d{N/d})=Nd=1N1d=1Nd{N/d}\sum_{n=1}^{N}\sigma(n) = \sum_{d=1}^{N} d \lfloor N/d \rfloor = \sum_{d=1}^{N} d(N/d - \{N/d\}) = N \sum_{d=1}^{N} 1 - \sum_{d=1}^{N} d\{N/d\}. More precisely, d=1NdN/d=Nd=1N1+\sum_{d=1}^{N} d\lfloor N/d\rfloor = N\sum_{d=1}^N 1 + \ldots. The standard computation via d=1NN/d/d\sum_{d=1}^{N}\lfloor N/d\rfloor/d and the asymptotic d=1N1/d2π2/6\sum_{d=1}^{N} 1/d^2 \to \pi^2/6 gives the leading term π212N2\frac{\pi^2}{12}N^2. \square

Lemma 2 (Sieve Range Bound). For nNn \leq N, σ(n)nHnN(1+lnN)\sigma(n) \leq n \cdot H_n \leq N \cdot (1 + \ln N), where HnH_n is the nn-th harmonic number. Thus the sieve for σ(σ(n))\sigma(\sigma(n)) must extend to M=O(NlogN)M = O(N \log N).

Proof. σ(n)/n=dn1/dd=1n1/d=Hn1+lnn\sigma(n)/n = \sum_{d \mid n} 1/d \leq \sum_{d=1}^{n} 1/d = H_n \leq 1 + \ln n. Therefore σ(n)n(1+lnn)N(1+lnN)\sigma(n) \leq n(1 + \ln n) \leq N(1 + \ln N). \square

Editorial

sigma(n) = sum of divisors of n. S(N) = sum_{n=1}^{N} sigma(sigma(n)). Method 1: Sieve for sigma, then lookup Method 2: Trial division (verification). We sieve sigma(n) for n = 1..M where M = max possible sigma(n). Finally, else.

Pseudocode

Sieve sigma(n) for n = 1..M where M = max possible sigma(n)
Compute S(N) = sum of sigma(sigma(n))
else

Complexity Analysis

  • Time: O(MlogM)O(M \log M) for the sieve where M=O(NlogN)M = O(N \log N), giving O(NlogNlog(NlogN))=O(Nlog2N)O(N \log N \cdot \log(N \log N)) = O(N \log^2 N). The summation is O(N)O(N) with table lookups.
  • Space: O(M)=O(NlogN)O(M) = O(N \log N) for the sieve array.

Answer

728378714\boxed{728378714}

Code

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

C++ project_euler/problem_632/solution.cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

/*
 * Problem 632: Sum of Divisors of Sum of Divisors
 *
 * sigma(n) = sum of divisors.
 * Compute S(N) = sum_{n=1}^{N} sigma(sigma(n)).
 *
 * Method 1: Sieve for sigma, then lookup/compute
 * Method 2: Trial division (verification)
 */

const int MAXN = 100001;
ll sig[MAXN];

void sieve_sigma(int N) {
    memset(sig, 0, sizeof(sig));
    for (int d = 1; d <= N; d++)
        for (int m = d; m <= N; m += d)
            sig[m] += d;
}

ll sigma_trial(ll n) {
    ll total = 0;
    for (ll d = 1; d * d <= n; d++) {
        if (n % d == 0) {
            total += d;
            if (d != n / d) total += n / d;
        }
    }
    return total;
}

int main() {
    int N = 100000;
    sieve_sigma(N);

    // Verify
    for (int n = 1; n <= 1000; n++)
        assert(sig[n] == sigma_trial(n));

    // Compute S(N)
    ll S = 0;
    for (int n = 1; n <= N; n++) {
        ll sn = sig[n];
        if (sn <= N) S += sig[sn];
        else S += sigma_trial(sn);
    }

    cout << "S(" << N << ") = " << S << endl;
    return 0;
}