Post · 3 min read· pp. 61–63

Number of divisors / sum of divisors

In this article we discuss how to compute the number of divisors d(n)d(n) and the sum of divisors σ(n)\sigma(n) of a given number nn.

Number of divisors

The prime factors of a divisor dd, including their multiplicities, have to form a submultiset of the prime factors of nn; e.g. 6=2⋅36 = 2 \cdot 3 is a divisor of 60=22⋅3⋅560 = 2^2 \cdot 3 \cdot 5. So we only need to count all different submultisets of the prime factorization of nn.

A set with xx distinct elements has 2x2^x subsets. In our case, however, some prime factors may appear multiple times in the prime factorization of nn, so we are choosing from a multiset rather than a set.

If a prime factor pp appears ee times in the prime factorization of nn, then we can use the factor pp up to ee times in the submultiset. Which means we have e+1e+1 choices.

Therefore if the prime factorization of nn is p1e1⋅p2e2⋯pkekp_1^{e_1} \cdot p_2^{e_2} \cdots p_k^{e_k}, where pip_i are distinct prime numbers, then the number of divisors is:

d(n)=(e1+1)⋅(e2+1)⋯(ek+1)d(n) = (e_1 + 1) \cdot (e_2 + 1) \cdots (e_k + 1)

A way of thinking about it is the following:

  • If there is only one distinct prime divisor n=p1e1n = p_1^{e_1}, then there are obviously e1+1e_1 + 1 divisors (1,p1,p12,…,p1e11, p_1, p_1^2, \dots, p_1^{e_1}).
  • If there are two distinct prime divisors n=p1e1⋅p2e2n = p_1^{e_1} \cdot p_2^{e_2}, then you can arrange all divisors in form of a tabular.

1p2p22…p2e211p2p22…p2e2p1p1p1⋅p2p1⋅p22…p1⋅p2e2p12p12p12⋅p2p12⋅p22…p12⋅p2e2⋮⋮⋮⋮⋱⋮p1e1p1e1p1e1⋅p2p1e1⋅p22…p1e1⋅p2e2\begin{array}{c|ccccc} & 1 & p_2 & p_2^2 & \dots & p_2^{e_2} \\\\\hline 1 & 1 & p_2 & p_2^2 & \dots & p_2^{e_2} \\\\ p_1 & p_1 & p_1 \cdot p_2 & p_1 \cdot p_2^2 & \dots & p_1 \cdot p_2^{e_2} \\\\ p_1^2 & p_1^2 & p_1^2 \cdot p_2 & p_1^2 \cdot p_2^2 & \dots & p_1^2 \cdot p_2^{e_2} \\\\ \vdots & \vdots & \vdots & \vdots & \ddots & \vdots \\\\ p_1^{e_1} & p_1^{e_1} & p_1^{e_1} \cdot p_2 & p_1^{e_1} \cdot p_2^2 & \dots & p_1^{e_1} \cdot p_2^{e_2} \\\\ \end{array}

So the number of divisors is trivially (e1+1)⋅(e2+1)(e_1 + 1) \cdot (e_2 + 1).

  • A similar argument can be made if there are more than two distinct prime factors.
text
long long numberOfDivisors(long long num) {
    long long total = 1;
    for (int i = 2; (long long)i * i <= num; i++) {
        if (num % i == 0) {
            int e = 0;
            do {
                e++;
                num /= i;
            } while (num % i == 0);
            total *= e + 1;
        }
    }
    if (num > 1) {
        total *= 2;
    }
    return total;
}

Sum of divisors

We can use the same argument of the previous section.

  • If there is only one distinct prime divisor n=p1e1n = p_1^{e_1}, then the sum is:

1+p1+p12+⋯+p1e1=p1e1+1−1p1−11 + p_1 + p_1^2 + \dots + p_1^{e_1} = \frac{p_1^{e_1 + 1} - 1}{p_1 - 1}

  • If there are two distinct prime divisors n=p1e1⋅p2e2n = p_1^{e_1} \cdot p_2^{e_2}, then we can make the same table as before. The only difference is that now we want to compute the sum instead of counting the elements. It is easy to see, that the sum of each combination can be expressed as:

(1+p1+p12+⋯+p1e1)⋅(1+p2+p22+⋯+p2e2)\left(1 + p_1 + p_1^2 + \dots + p_1^{e_1}\right) \cdot \left(1 + p_2 + p_2^2 + \dots + p_2^{e_2}\right)
=p1e1+1−1p1−1⋅p2e2+1−1p2−1= \frac{p_1^{e_1 + 1} - 1}{p_1 - 1} \cdot \frac{p_2^{e_2 + 1} - 1}{p_2 - 1}

  • In general, for n=p1e1⋅p2e2⋯pkekn = p_1^{e_1} \cdot p_2^{e_2} \cdots p_k^{e_k} we receive the formula:

σ(n)=p1e1+1−1p1−1⋅p2e2+1−1p2−1⋯pkek+1−1pk−1\sigma(n) = \frac{p_1^{e_1 + 1} - 1}{p_1 - 1} \cdot \frac{p_2^{e_2 + 1} - 1}{p_2 - 1} \cdots \frac{p_k^{e_k + 1} - 1}{p_k - 1}

text
long long SumOfDivisors(long long num) {
    long long total = 1;

    for (int i = 2; (long long)i * i <= num; i++) {
        if (num % i == 0) {
            int e = 0;
            do {
                e++;
                num /= i;
            } while (num % i == 0);

            long long sum = 0, pow = 1;
            do {
                sum += pow;
                pow *= i;
            } while (e-- > 0);
            total *= sum;
        }
    }
    if (num > 1) {
        total *= (1 + num);
    }
    return total;
}

Multiplicative functions

A multiplicative function is a function f(x)f(x) which satisfies

f(a⋅b)=f(a)⋅f(b)f(a \cdot b) = f(a) \cdot f(b)

if aa and bb are coprime.

Both d(n)d(n) and σ(n)\sigma(n) are multiplicative functions.

Multiplicative functions have a huge variety of interesting properties, which can be very useful in number theory problems. For instance the Dirichlet convolution of two multiplicative functions is also multiplicative.

Practice Problems