Sum-of-divisors function (source code)

= Sum-of-divisors function
{title2=$\sigma_1(n)$}

= Divisor-sum function
{synonym}

The <sum-of-divisors function> adds all positive <integer divisors> of a <positive integer>, including the number itself:
$$
\sigma(n)=\sigma_1(n)=\sum_{d\mid n}d,\qquad \sigma(1)=1.
$$
It is the <divisor sum> of the <identity arithmetic function> $\operatorname{id}(n)=n$, so $\sigma=\operatorname{id}*\mathbf1$ under <Dirichlet convolution>. Both factors are <multiplicative functions>, hence so is the <sum-of-divisors function>. The notation $\sigma_1$ distinguishes it from other weighted <divisor sums>. https://darijgrinberg.gitlab.io/floor.pdf[Darij Grinberg, MIT 18.781 notes, Definition 2.1.2 and Proposition 2.2.2(e)]{ref}

For a <prime number> $p$ and a nonnegative exponent $a$, the positive <integer divisors> of the <prime power> $p^a$ are precisely $1,p,\ldots,p^a$. Their <finite geometric series> gives
$$
\sigma(p^a)=1+p+\cdots+p^a=\frac{p^{a+1}-1}{p-1}.
$$
For example, $\sigma(8)=1+2+4+8=15=(2^4-1)/(2-1)$. For a <prime factorization> $n=\prod_{k=1}^r p_k^{a_k}$ with distinct <prime numbers> $p_k$ and positive exponents $a_k$, <multiplicativity> gives
$$
\sigma(n)=\prod_{k=1}^r(1+p_k+\cdots+p_k^{a_k})=\prod_{k=1}^r\frac{p_k^{a_k+1}-1}{p_k-1}.
$$
For $n=1$, this is an <empty product>, equal to $1$. https://darijgrinberg.gitlab.io/floor.pdf[Darij Grinberg, MIT 18.781 notes, Theorem 2.1.8 with k equal to one]{ref}

The <distributive property> makes the formula especially beautiful: choosing one term from each factor produces each positive <integer divisor> exactly once, by the <fundamental theorem of arithmetic>. For $12=4\cdot3=2^2\cdot3$, the choices form a three-by-two grid with entries $2^i3^j$, where $0\le i\le2$ and $0\le j\le1$. Adding its rows gives
$$
\sigma(12)=(1+2+4)+(3+6+12)=(1+2+4)(1+3)=7\cdot4=28.
$$
The <divisor function> counts six cells; the <sum-of-divisors function> adds their six values. Once the <prime factorization> is known, the product formula avoids testing every candidate <integer divisor>. Shared <prime factors> require care: $\sigma(4)=7\ne9=\sigma(2)^2$, so the <sum-of-divisors function> is not a <completely multiplicative function>.

\Image[/divisor-sum-prime-factor-grid.png]
{title=The <sum-of-divisors function> for $12$: each grid entry is a positive <integer divisor>, and the weighted total is $(1+2+4)(1+3)=28$}

Subtracting the number itself gives the <proper-divisor sum> $s(n)=\sigma(n)-n$. Comparing $s(n)$ with $n$ distinguishes <perfect numbers>, <deficient numbers>, and <abundant numbers>. For $6$, $s(6)=1+2+3=6$; for $28$, $s(28)=1+2+4+7+14=28$. Both are <perfect numbers>. The calculation $s(8)=1+2+4=7<8$ makes $8$ a <deficient number>, while $s(12)=1+2+3+4+6=16>12$ makes $12$ an <abundant number>. These comparisons turn the sizes of <integer divisors> into a classification of <positive integers>. https://ocw.mit.edu/courses/ec-s01-internet-technology-in-local-and-global-communities-spring-2005-summer-2005/aac0b504648aff57a83daec957da3a58_MITEC_S01S05_perf_numb.pdf[Western Carolina University, Challenge Problem: Perfect Numbers, hosted by MIT OpenCourseWare]{ref}

\Image[/proper-divisor-sum-classification.png]
{title=The <proper-divisor sum> for $1\le n\le30$: <perfect numbers> lie on $s(n)=n$, <deficient numbers> below it, and <abundant numbers> above it}

This connection is ancient: <Euclid's Elements>, Book IX, Proposition 36, constructs a <perfect number> by multiplying the last term of $1,2,\ldots,2^{k-1}$ by their <sum>, when that <sum> is a <prime number>. In modern notation, put $q=2^k-1$ and $N=2^{k-1}q$, with $k\ge2$ and $q$ a <prime number>. The <sum-of-divisors function> proves the construction immediately:
$$
\sigma(N)=(2^k-1)(1+q)=q\,2^k=2N.
$$
Consequently the <proper-divisor sum> is $s(N)=N$. Taking $k=2,3$ produces the <perfect numbers> $6,28$. <Euclid's Elements by Euclid/Book IX, Proposition 36>[Euclid, Elements IX.36, translated text and commentary by David Joyce]{ref}