All Euler problems
Project Euler

Amicable Numbers

Let d(n) denote the sum of proper divisors of n (i.e., divisors strictly less than n). If d(a) = b and d(b) = a with a!= b, then a and b form an amicable pair. Compute the sum of all amicable numbe...

Source sync Apr 19, 2026
Problem #0021
Level Level 00
Solved By 159,690
Languages C++, Python
Answer 31626
Length 391 words
number_theorymodular_arithmeticbrute_force

Problem Statement

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

Let \(d(n)\) be defined as the sum of proper divisors of \(n\) (numbers less than \(n\) which divide evenly into \(n\)).

If \(d(a) = b\) and \(d(b) = a\), where \(a \ne b\), then \(a\) and \(b\) are an amicable pair and each of \(a\) and \(b\) are called amicable numbers.

For example, the proper divisors of \(220\) are \(1, 2, 4, 5, 10, 11, 20, 22, 44, 55\) and \(110\); therefore \(d(220) = 284\). The proper divisors of \(284\) are \(1, 2, 4, 71\) and \(142\); so \(d(284) = 220\).

Evaluate the sum of all the amicable numbers under \(10000\).

Problem 21: Amicable Numbers

Mathematical Development

Definitions

Definition 1 (Divisor-Sum Functions). For nZ>0n \in \mathbb{Z}_{>0}, the divisor-sum function is

σ1(n)=dnd,\sigma_1(n) = \sum_{d \mid n} d,

and the restricted divisor-sum function (sum of proper divisors) is s(n)=σ1(n)ns(n) = \sigma_1(n) - n.

Definition 2 (Amicable Pair). An ordered pair (a,b)Z>02(a, b) \in \mathbb{Z}_{>0}^2 with aba \neq b is amicable if s(a)=bs(a) = b and s(b)=as(b) = a.

Theorems and Proofs

Theorem 1 (Multiplicativity of σ1\sigma_1). The function σ1\sigma_1 is multiplicative: if gcd(m,n)=1\gcd(m, n) = 1, then σ1(mn)=σ1(m)σ1(n)\sigma_1(mn) = \sigma_1(m)\,\sigma_1(n). Consequently, for n=i=1kpiain = \prod_{i=1}^{k} p_i^{a_i},

σ1(n)=i=1kpiai+11pi1.\sigma_1(n) = \prod_{i=1}^{k} \frac{p_i^{a_i+1} - 1}{p_i - 1}.

Proof. Let gcd(m,n)=1\gcd(m, n) = 1. The map (d1,d2)d1d2(d_1, d_2) \mapsto d_1 d_2 is a bijection from {d1:d1m}×{d2:d2n}\{d_1 : d_1 \mid m\} \times \{d_2 : d_2 \mid n\} to {d:dmn}\{d : d \mid mn\}. (Surjectivity follows from the unique factorization in Z\mathbb{Z} and the coprimality condition; injectivity is immediate.) Therefore

σ1(mn)=dmnd=d1m  d2nd1d2=(d1md1)(d2nd2)=σ1(m)σ1(n).\sigma_1(mn) = \sum_{d \mid mn} d = \sum_{d_1 \mid m}\;\sum_{d_2 \mid n} d_1 d_2 = \biggl(\sum_{d_1 \mid m} d_1\biggr)\biggl(\sum_{d_2 \mid n} d_2\biggr) = \sigma_1(m)\,\sigma_1(n).

For a prime power pap^a, the complete list of divisors is {1,p,p2,,pa}\{1, p, p^2, \ldots, p^a\}, so

σ1(pa)=j=0apj=pa+11p1\sigma_1(p^a) = \sum_{j=0}^{a} p^j = \frac{p^{a+1} - 1}{p - 1}

by the closed form of the geometric series. The product formula follows by induction on the number of distinct prime factors, applying multiplicativity at each step. \square

Lemma 1 (Trial-Division Computation of s(n)s(n)). The value s(n)s(n) can be computed in O(n)O(\sqrt{n}) arithmetic operations.

Proof. Every divisor dd of nn satisfies either dnd \leq \sqrt{n} or n/dnn/d \leq \sqrt{n} (or both, when d=nd = \sqrt{n}). Hence divisors pair as (d,n/d)(d, n/d) for dnd \leq \sqrt{n}, and we may write

σ1(n)=d=1dnn(d+nd)ϵ(n),ϵ(n)={nif n2=n,0otherwise.\sigma_1(n) = \sum_{\substack{d=1 \\ d \mid n}}^{\lfloor\sqrt{n}\rfloor} \left(d + \frac{n}{d}\right) - \epsilon(n), \qquad \epsilon(n) = \begin{cases} \sqrt{n} & \text{if } \lfloor\sqrt{n}\rfloor^2 = n, \\ 0 & \text{otherwise.} \end{cases}

The correction ϵ(n)\epsilon(n) avoids double-counting the case d=n/dd = n/d. The loop performs n\lfloor\sqrt{n}\rfloor iterations, each requiring O(1)O(1) work (one division, one comparison), yielding O(n)O(\sqrt{n}) total. Then s(n)=σ1(n)ns(n) = \sigma_1(n) - n. \square

Theorem 2 (Enumeration). The complete set of amicable pairs (a,b)(a, b) with a<b<10,000a < b < 10{,}000 is:

{(220,284),  (1184,1210),  (2620,2924),  (5020,5564),  (6232,6368)}.\{(220, 284),\; (1184, 1210),\; (2620, 2924),\; (5020, 5564),\; (6232, 6368)\}.

Proof. For each integer nn with 2n<10,0002 \leq n < 10{,}000, compute m=s(n)m = s(n). If mnm \neq n, m>0m > 0, and s(m)=ns(m) = n, record the pair {min(n,m),max(n,m)}\{\min(n,m), \max(n,m)\}. This exhaustive, finite enumeration produces exactly the five pairs listed. (The computation is deterministic and terminates in at most 10,00010{,}000 evaluations of ss.) \square

Editorial

We enumerate every integer n<Nn < N and test the amicable condition directly. The helper s(n)s(n) computes the sum of proper divisors by scanning divisor pairs up to n\sqrt{n}, then the main loop sets m=s(n)m = s(n) and checks whether mnm \ne n and s(m)=ns(m) = n; if so, nn is added to the total. This is sufficient because the definition of an amicable number is exactly the symmetric condition being tested.

Pseudocode

Algorithm: Sum of Amicable Numbers Below a Bound
Require: An integer N ≥ 2.
Ensure: The sum of all amicable numbers less than N.
1: Initialize total ← 0.
2: For each n in {2, 3, ..., N - 1}, compute m ← s(n), where s(x) denotes the sum of the proper divisors of x.
3: If m = n, continue to the next value; otherwise compute s(m).
4: If s(m) = n, update total ← total + n.
5: Return total.

Complexity Analysis

Proposition. Algorithm SumAmicable(N) runs in O(NN)O(N\sqrt{N}) time and O(1)O(1) auxiliary space.

Proof. The outer loop iterates N2N - 2 times. Each iteration calls s(n)s(n) at most twice (once for s(n)s(n) and conditionally once for s(m)s(m)). By Lemma 1, each call to ss costs O(N)O(\sqrt{N}) in the worst case (since both n<Nn < N and m=O(NloglogN)m = O(N \log\log N) by the maximal order of σ1\sigma_1, but m=O(NloglogN)=O(N)\sqrt{m} = O(\sqrt{N \log\log N}) = O(\sqrt{N}) asymptotically). Thus total work is O(NN)=O(N3/2)O(N \cdot \sqrt{N}) = O(N^{3/2}). No arrays are required beyond loop variables, so auxiliary space is O(1)O(1).

Alternative. A divisor-sum sieve (iterating each ii and adding ii to s(j)s(j) for all multiples j=2i,3i,j = 2i, 3i, \ldots) computes all values s(n)s(n) for nNn \leq N in O(NlogN)O(N \log N) time and O(N)O(N) space, yielding an O(NlogN)O(N \log N) algorithm overall. \square

Answer

31626\boxed{31626}

Code

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

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

int sum_proper_divisors(int n) {
    if (n <= 1) return 0;
    int s = 1;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            s += i;
            if (i != n / i) s += n / i;
        }
    }
    return s;
}

int main() {
    int total = 0;
    for (int a = 2; a < 10000; a++) {
        int b = sum_proper_divisors(a);
        if (b != a && b > 0 && sum_proper_divisors(b) == a)
            total += a;
    }
    cout << total << endl;
    return 0;
}