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...
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 , the divisor-sum function is
and the restricted divisor-sum function (sum of proper divisors) is .
Definition 2 (Amicable Pair). An ordered pair with is amicable if and .
Theorems and Proofs
Theorem 1 (Multiplicativity of ). The function is multiplicative: if , then . Consequently, for ,
Proof. Let . The map is a bijection from to . (Surjectivity follows from the unique factorization in and the coprimality condition; injectivity is immediate.) Therefore
For a prime power , the complete list of divisors is , so
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.
Lemma 1 (Trial-Division Computation of ). The value can be computed in arithmetic operations.
Proof. Every divisor of satisfies either or (or both, when ). Hence divisors pair as for , and we may write
The correction avoids double-counting the case . The loop performs iterations, each requiring work (one division, one comparison), yielding total. Then .
Theorem 2 (Enumeration). The complete set of amicable pairs with is:
Proof. For each integer with , compute . If , , and , record the pair . This exhaustive, finite enumeration produces exactly the five pairs listed. (The computation is deterministic and terminates in at most evaluations of .)
Editorial
We enumerate every integer and test the amicable condition directly. The helper computes the sum of proper divisors by scanning divisor pairs up to , then the main loop sets and checks whether and ; if so, 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 time and auxiliary space.
Proof. The outer loop iterates times. Each iteration calls at most twice (once for and conditionally once for ). By Lemma 1, each call to costs in the worst case (since both and by the maximal order of , but asymptotically). Thus total work is . No arrays are required beyond loop variables, so auxiliary space is .
Alternative. A divisor-sum sieve (iterating each and adding to for all multiples ) computes all values for in time and space, yielding an algorithm overall.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#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;
}
def sum_proper_divisors(n):
if n <= 1:
return 0
s = 1
i = 2
while i * i <= n:
if n % i == 0:
s += i
if i != n // i:
s += n // i
i += 1
return s
def solve():
LIMIT = 10000
total = 0
for a in range(2, LIMIT):
b = sum_proper_divisors(a)
if b != a and b > 0 and sum_proper_divisors(b) == a:
total += a
print(total)