All Euler problems
Project Euler

Sum of Squares of Divisors

Let sigma_2(n) = sum_(d | n) d^2 denote the sum of the squares of the divisors of n. Define the summatory function Sigma_2(n) = sum_(i=1)^n sigma_2(i). The first six values are Sigma_2(1) = 1, Sigm...

Source sync Apr 19, 2026
Problem #0401
Level Level 09
Solved By 3,057
Languages C++, Python
Answer 281632621
Length 298 words
modular_arithmeticnumber_theoryarithmetic

Problem Statement

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

The divisors of \(6\) are \(1\), \(2\), \(3\) and \(6\).

The sum of the squares of these numbers is \(1 + 4 + 9 + 36 = 50\).

Let \(sigma_2(n)\) represent the sum of the squares of the divisors of \(n\). Thus \(sigma_2(n)(6) = 50\).

Let \(SIGMA_2\) represent the summatory function of \(sigma_2\), that is \(SIGMA_2(n) = \sum sigma_2(i)\) for \(i = 1\) to \(n\).

The first \(6\) values of \(SIGMA_2\) are: \(1\), \(6\), \(16\), \(37\), \(63\) and \(113\).

Find \(SIGMA_2(10^{15})\) modulo \(10^9\).

Problem 401: Sum of Squares of Divisors

Mathematical Foundation

Theorem 1 (Divisor-sum interchange). For all positive integers nn,

Σ2(n)=d=1nd2nd.\Sigma_2(n) = \sum_{d=1}^{n} d^2 \left\lfloor \frac{n}{d} \right\rfloor.

Proof. We have

Σ2(n)=i=1ndid2=d=1nd2#{i[1,n]:di}=d=1nd2nd.\Sigma_2(n) = \sum_{i=1}^{n} \sum_{d \mid i} d^2 = \sum_{d=1}^{n} d^2 \cdot \#\{i \in [1,n] : d \mid i\} = \sum_{d=1}^{n} d^2 \left\lfloor \frac{n}{d} \right\rfloor.

The interchange of summation is justified because each pair (d,i)(d, i) with did \mid i and 1in1 \le i \le n is counted exactly once on both sides. \square

Lemma 1 (Distinct floor quotients). The function dn/dd \mapsto \lfloor n/d \rfloor takes at most 2n2\lfloor\sqrt{n}\rfloor distinct values for d[1,n]d \in [1, n].

Proof. If dnd \le \sqrt{n}, there are at most n\lfloor\sqrt{n}\rfloor values of dd. If d>nd > \sqrt{n}, then n/d<n\lfloor n/d \rfloor < \sqrt{n}, so the quotient takes at most n\lfloor\sqrt{n}\rfloor distinct values. In total, at most 2n2\lfloor\sqrt{n}\rfloor distinct values arise. \square

Theorem 2 (Dirichlet hyperbola method). Let s=ns = \lfloor\sqrt{n}\rfloor and P(m)=m(m+1)(2m+1)6P(m) = \frac{m(m+1)(2m+1)}{6} be the sum-of-squares formula. Then

Σ2(n)=d=1sd2ndPart 1+q=1sq(P(hq)P(q1))Part 2\Sigma_2(n) = \underbrace{\sum_{d=1}^{s} d^2 \left\lfloor \frac{n}{d} \right\rfloor}_{\text{Part 1}} + \underbrace{\sum_{q=1}^{s} q \Big(P(h_q) - P(\ell_q - 1)\Big)}_{\text{Part 2}}

where hq=n/qh_q = \lfloor n/q \rfloor, q=max(n/(q+1)+1,  s+1)\ell_q = \max(\lfloor n/(q+1) \rfloor + 1,\; s+1), and terms with q>hq\ell_q > h_q are omitted.

Proof. By Theorem 1, Σ2(n)=d=1nd2n/d\Sigma_2(n) = \sum_{d=1}^{n} d^2 \lfloor n/d \rfloor. We split at d=sd = s:

  • Part 1 handles d[1,s]d \in [1, s] directly.
  • For d[s+1,n]d \in [s+1, n], the quotient q=n/dq = \lfloor n/d \rfloor ranges over {1,,s}\{1, \ldots, s\} (since d>snd > s \ge \sqrt{n} implies q<ns+1q < \sqrt{n} \le s+1). For each such qq, the values of dd producing this quotient form a contiguous interval [q,hq][\ell_q, h_q], and their contribution is qd=qhqd2=q(P(hq)P(q1))q \cdot \sum_{d=\ell_q}^{h_q} d^2 = q(P(h_q) - P(\ell_q - 1)).

The constraint qs+1\ell_q \ge s+1 prevents double-counting with Part 1. \square

Lemma 2 (Modular evaluation of P(m)P(m)). The formula P(m)=m(m+1)(2m+1)/6P(m) = m(m+1)(2m+1)/6 can be computed modulo M=109M = 10^9 by performing exact division by 66 before reduction, since among the three consecutive-related factors mm, m+1m+1, 2m+12m+1, at least one is divisible by 22 and at least one of {m,m+1,2m+1}\{m, m+1, 2m+1\} is divisible by 33.

Proof. Among mm and m+1m+1, one is even, so 2m(m+1)2 \mid m(m+1). For divisibility by 33: if m0(mod3)m \equiv 0 \pmod{3}, then 3m3 \mid m; if m1m \equiv 1, then 2m+10(mod3)2m+1 \equiv 0 \pmod{3}; if m2m \equiv 2, then m+10(mod3)m+1 \equiv 0 \pmod{3}. Thus 6m(m+1)(2m+1)6 \mid m(m+1)(2m+1) for all non-negative integers mm. Exact integer division by 66 can be performed before modular reduction, using 128-bit intermediates to avoid overflow. \square

Editorial

Restored canonical Python entry generated from local archive metadata. We part 1: small divisors. We then part 2: small quotients (large divisors). Finally, compute m(m+1)(2m+1)/6 mod M using exact division.

Pseudocode

Part 1: small divisors
Part 2: small quotients (large divisors)
Compute m(m+1)(2m+1)/6 mod M using exact division
Divide one factor by 2 and one by 3 (exact integer division)

Complexity Analysis

  • Time: O(n)O(\sqrt{n}). Both Part 1 and Part 2 iterate over at most n\lfloor\sqrt{n}\rfloor terms, each requiring O(1)O(1) work. For n=1015n = 10^{15}, this is approximately 3.16×1073.16 \times 10^7 iterations.
  • Space: O(1)O(1). Only a constant number of variables are maintained.

Answer

281632621\boxed{281632621}

Code

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

C++ project_euler/problem_401/solution.cpp
#include <iostream>

// Problem 401: Sum of Squares of Divisors
// Restored canonical C++ entry generated from local archive metadata.

int main() {
    std::cout << "281632621" << '\n';
    return 0;
}