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 May 21, 2026
Problem #0211
Level Level 07
Solved By 4,801
Languages C++, Python
Answer 1922364685
Length 465 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 k∈Z≥0k \in \mathbb{Z}_{\geq 0}, the divisor power sum function is σk(n)=∑d∣ndk\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,b∈Z>0a, b \in \mathbb{Z}_{>0} with gcd⁡(a,b)=1\gcd(a, b) = 1. We claim the map

φ ⁣:{d1:d1∣a}×{d2:d2∣b}→{d:d∣ab},φ(d1,d2)=d1d2\varphi \colon \{d_1 : d_1 \mid a\} \times \{d_2 : d_2 \mid b\} \to \{d : d \mid ab\}, \qquad \varphi(d_1, d_2) = d_1 d_2

is a bijection.

Surjectivity. Let d∣abd \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 aa or bb. Set

d1=∏p∣apep,d2=∏p∣bpep.d_1 = \prod_{p \mid a} p^{e_p}, \qquad d_2 = \prod_{p \mid b} p^{e_p}.

Then d1∣ad_1 \mid a, d2∣bd_2 \mid b, and d=d1d2d = d_1 d_2.

Injectivity. If d1d2=d1′d2′d_1 d_2 = d_1' d_2' with d1,d1′∣ad_1, d_1' \mid a and d2,d2′∣bd_2, d_2' \mid b, then gcd⁡(d1,d2′)=1\gcd(d_1, d_2') = 1, so d1∣d1′d_1 \mid d_1'. By symmetry d1′∣d1d_1' \mid d_1, hence d1=d1′d_1 = d_1' and then d2=d2′d_2 = d_2'.

Therefore

σ2(ab)=∑d∣abd2=∑d1∣a∑d2∣b(d1d2)2=(∑d1∣ad12)(∑d2∣bd22)=σ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)\sigma_2(b). \qquad \square

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

σ2(pa)=∑j=0ap2j=p2(a+1)−1p2−1.\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},

which is a finite geometric series with ratio p2p^2. □\square

Proposition 1 (Overflow bound). For n<N=6.4×107n < N = 6.4 \times 10^7, unsigned 64-bit integers suffice to store σ2(n)\sigma_2(n) in the sieve implementation.

Theorem 2 (Segmented factorization formula). Let

n=∏i=1rpiain = \prod_{i=1}^{r} p_i^{a_i}

be the prime factorization of nn. If we process each prime factor pip_i once, multiply an accumulator by

1+pi2+pi4+⋯+pi2ai,1 + p_i^2 + p_i^4 + \cdots + p_i^{2a_i},

and remove piaip_i^{a_i} from a working copy of nn, then the final accumulator equals σ2(n)\sigma_2(n). If a residual factor remains after all primes up to n\sqrt{n} have been removed, that residual factor is prime and contributes a final factor 1+q21 + q^2.

Proof. The product formula

σ2(n)=∏i=1rσ2(piai)=∏i=1r(1+pi2+pi4+⋯+pi2ai)\sigma_2(n) = \prod_{i=1}^{r} \sigma_2(p_i^{a_i}) = \prod_{i=1}^{r} \left(1 + p_i^2 + p_i^4 + \cdots + p_i^{2a_i}\right)

follows immediately from Theorem 1 and Lemma 1.

Now remove the prime powers from a working copy of nn. When all primes up to n\sqrt{n} have been processed, any remaining factor q>1q > 1 cannot be composite, because a composite number has a prime divisor at most its square root. Therefore the remaining factor is either 11 or a single prime, and multiplying by 1+q21 + q^2 completes the product for σ2(n)\sigma_2(n). □\square

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

Editorial

The useful structural fact is that σ2\sigma_2 is multiplicative, so once the prime factorization of nn is known we can recover σ2(n)\sigma_2(n) from the geometric-series factors

1+p2+p4+⋯+p2a.1 + p^2 + p^4 + \cdots + p^{2a}.

Factoring each integer from scratch would still be too slow, so the implementation works in blocks. Inside one block we keep the current values of the integers and an accumulator for their σ2\sigma_2 product. Then, for each prime p≤Np \le \sqrt{N}, we walk through the multiples of pp inside that block, strip off the full power of pp, and multiply the accumulator by the corresponding prime-power contribution.

After all small primes are processed, each number has at most one prime factor left. Multiplying by 1+q21 + q^2 finishes σ2(n)\sigma_2(n), and then a single integer-square-root check tells us whether that value is a square. This keeps the same mathematics as the direct formula, but avoids storing a length-64,000,00064{,}000{,}000 divisor table in Python.

Pseudocode

Set N = 64,000,000 and choose a block size B.
Generate all primes up to sqrt(N - 1).

answer = 0
For each block [L, R):
    values[i] = L + i for every index in the block
    sigma2[i] = 1 for every index in the block

    For each prime p in the prime list:
        Find the first multiple of p inside [L, R)

        For each multiple m of p inside the block:
            contribution = 1
            power = 1

            While values[m - L] is divisible by p:
                Divide values[m - L] by p
                power = power * p^2
                contribution = contribution + power

            Multiply sigma2[m - L] by contribution

    For each position i in the block:
        If values[i] > 1:
            Multiply sigma2[i] by 1 + values[i]^2

        r = integer square root of sigma2[i]
        If r * r = sigma2[i]:
            answer += L + i

Return answer

Complexity Analysis

  • Time: Roughly O(Nlog⁡log⁡N)O(N \log \log N) arithmetic over all blocks. Each prime visits its multiples, and each prime factor is removed only as many times as its exponent occurs.
  • Space: O(B+π(N))O(B + \pi(\sqrt{N})), where BB is the chosen block size.

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