All Euler problems
Project Euler

Divisor Square Sum

For a positive integer n, let sigma_2(n) denote the sum of the squares of the divisors of n: sigma_2(n) = sum_(d | n) d^2 For example, sigma_2(10) = 1^2 + 2^2 + 5^2 + 10^2 = 130. Find the sum of al...

Source sync Apr 19, 2026
Problem #0211
Level Level 07
Solved By 4,801
Languages C++, Python
Answer 1922364685
Length 321 words
number_theorybrute_forcelinear_algebra

Problem Statement

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

For a positive integer \(n\), let \(\sigma _2(n)\) be the sum of the squares of its divisors. For example, \[\sigma _2(10) = 1 + 4 + 25 + 100 = 130.\] Find the sum of all \(n\), \(0 < n < 64\,000\,000\) such that \(\sigma _2(n)\) is a perfect square.

Problem 211: Divisor Square Sum

Mathematical Development

Definition 1. For kZ0k \in \mathbb{Z}_{\geq 0}, the divisor power sum function is σk(n)=dndk\sigma_k(n) = \sum_{d \mid n} d^k. The case k=2k = 2 gives the sum-of-squares-of-divisors function.

Theorem 1 (Multiplicativity of σ2\sigma_2). The function σ2\sigma_2 is multiplicative: if gcd(a,b)=1\gcd(a, b) = 1, then σ2(ab)=σ2(a)σ2(b)\sigma_2(ab) = \sigma_2(a) \cdot \sigma_2(b).

Proof. Let a,bZ>0a, b \in \mathbb{Z}_{>0} with gcd(a,b)=1\gcd(a, b) = 1. We claim the map φ ⁣:{d1:d1a}×{d2:d2b}{d:dab}\varphi \colon \{d_1 : d_1 \mid a\} \times \{d_2 : d_2 \mid b\} \to \{d : d \mid ab\} defined by φ(d1,d2)=d1d2\varphi(d_1, d_2) = d_1 d_2 is a bijection.

Surjectivity. Let dabd \mid ab. Write d=ppepd = \prod_p p^{e_p}. Since gcd(a,b)=1\gcd(a, b) = 1, each prime pp divides at most one of a,ba, b. Set d1=papepd_1 = \prod_{p \mid a} p^{e_p} and d2=pbpepd_2 = \prod_{p \mid b} p^{e_p}. Then d1ad_1 \mid a, d2bd_2 \mid b, and d=d1d2d = d_1 d_2.

Injectivity. If d1d2=d1d2d_1 d_2 = d_1' d_2' with d1,d1ad_1, d_1' \mid a and d2,d2bd_2, d_2' \mid b, then since gcd(a,b)=1\gcd(a, b) = 1 implies gcd(d1,d2)=1\gcd(d_1, d_2') = 1, we have d1d1d_1 \mid d_1'. By symmetry d1d1d_1' \mid d_1, so d1=d1d_1 = d_1' and thus d2=d2d_2 = d_2'.

Therefore:

σ2(ab)=dabd2=d1ad2b(d1d2)2=(d1ad12)(d2bd22)=σ2(a)σ2(b).\sigma_2(ab) = \sum_{d \mid ab} d^2 = \sum_{d_1 \mid a} \sum_{d_2 \mid b} (d_1 d_2)^2 = \left(\sum_{d_1 \mid a} d_1^2\right)\left(\sum_{d_2 \mid b} d_2^2\right) = \sigma_2(a) \cdot \sigma_2(b). \qquad \square

Lemma 1 (Geometric sum for prime powers). For a prime pp and integer a0a \geq 0,

σ2(pa)=j=0ap2j=p2(a+1)1p21.\sigma_2(p^a) = \sum_{j=0}^{a} p^{2j} = \frac{p^{2(a+1)} - 1}{p^2 - 1}.

Proof. The divisors of pap^a are precisely 1,p,p2,,pa1, p, p^2, \ldots, p^a. Hence σ2(pa)=j=0a(pj)2=j=0ap2j\sigma_2(p^a) = \sum_{j=0}^{a} (p^j)^2 = \sum_{j=0}^{a} p^{2j}. This is a finite geometric series with first term 11, common ratio p2p^2, and a+1a+1 terms, yielding the closed form (p2(a+1)1)/(p21)(p^{2(a+1)} - 1)/(p^2 - 1). \square

Proposition 1 (Overflow bound). For n<N=6.4×107n < N = 6.4 \times 10^7, we have σ2(n)σ2(n)1<n2d(n)\sigma_2(n) \leq \sigma_2(n) \cdot 1 < n^2 \cdot d(n) where d(n)d(n) is the number of divisors. Since d(n)=O(nϵ)d(n) = O(n^{\epsilon}) for any ϵ>0\epsilon > 0, and empirically σ2(n)<263\sigma_2(n) < 2^{63} for n<6.4×107n < 6.4 \times 10^7, unsigned 64-bit integers suffice.

Theorem 2 (Divisor sieve correctness). Let NZ>0N \in \mathbb{Z}_{>0}. Define the array S[1..N1]S[1..N-1] by initializing S[n]0S[n] \leftarrow 0 for all nn, then for each d=1,2,,N1d = 1, 2, \ldots, N-1 and each multiple k=d,2d,3d,k = d, 2d, 3d, \ldots with k<Nk < N, executing S[k]S[k]+d2S[k] \leftarrow S[k] + d^2. Upon completion, S[n]=σ2(n)S[n] = \sigma_2(n) for all 1n<N1 \leq n < N.

Proof. After the sieve terminates, S[n]=d=1dnN1d2S[n] = \sum_{\substack{d=1 \\ d \mid n}}^{N-1} d^2. Since 1n<N1 \leq n < N, every divisor dd of nn satisfies 1dn<N1 \leq d \leq n < N, so the sum ranges over exactly the divisor set of nn. Thus S[n]=dnd2=σ2(n)S[n] = \sum_{d \mid n} d^2 = \sigma_2(n). \square

Lemma 2 (Perfect square detection). A nonnegative integer ss is a perfect square if and only if s2=s\lfloor \sqrt{s} \rfloor^2 = s. Integer square root can be computed exactly via Newton’s method or built-in isqrt.

Editorial

We compute sigma_2 via divisor sieve. Finally, check perfect squares and accumulate. 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

Compute sigma_2 via divisor sieve
Check perfect squares and accumulate

Complexity Analysis

Time. The sieve loop executes d=1N1(N1)/d\sum_{d=1}^{N-1} \lfloor (N-1)/d \rfloor iterations. By the harmonic number estimate d=1N1/d=lnN+γ+O(1/N)\sum_{d=1}^{N} 1/d = \ln N + \gamma + O(1/N), this sum equals NlnN+O(N)N \ln N + O(N). The perfect-square check loop runs in Θ(N)\Theta(N). Total time complexity: Θ(NlogN)\Theta(N \log N).

Space. The array SS stores NN unsigned 64-bit values, requiring 8N8N bytes. For N=6.4×107N = 6.4 \times 10^7, this is approximately 512 MB. Auxiliary space is O(1)O(1). Total: Θ(N)\Theta(N).

Answer

1922364685\boxed{1922364685}

Code

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

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

int main() {
    const int N = 64000000;
    vector<unsigned long long> sigma2(N, 0);

    for (long long d = 1; d < N; d++) {
        long long d2 = d * d;
        for (long long k = d; k < N; k += d)
            sigma2[k] += d2;
    }

    long long answer = 0;
    for (int n = 1; n < N; n++) {
        unsigned long long s = sigma2[n];
        unsigned long long r = (unsigned long long)sqrt((double)s);
        while (r * r > s) r--;
        while ((r + 1) * (r + 1) <= s) r++;
        if (r * r == s)
            answer += n;
    }

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