The divisor function represented as d(n) counts the number of divisors of an integer.

Example: d(18)

The numbers that divide 18 are 1,2,3,6,9,18; then d(18)=6

Important observations

  • if p is a prime number, then d(p)=2; also, d(pk)=k+1 because every power of p is a divisor of pk, e.g., p0,p1,p2,,pk.
  • if n is a product of two distinct primes, say n=pq, then d(pq)=d(p)d(q); also, d(piqj)=d(pi)d(qj)
  • in general, let n=p1a1p2a2pnan, then d(n)=d(p1a1)d(p2a2)d(pnan) where pi is a prime factor that divides n.

example: d(18)

d(18)=d(322)=d(32)(2)=32=6
int number_of_divisors(int n) {
  int total = 1;
  for (int i = 2; i * i <= n; i += 1) {
    int power = 0;
    while (n % i == 0) {
      power += 1;
      n /= i;
    }
    total *= (power + 1);
  }
  if (n > 1){
    total *= 2;
  }
  return total;
}

Sum of divisors

The sum of divisors is another important quantity represented by σk(n). It’s the sum of the k-th powers of the divisors of n

σk(n)=d|ndk

Examples:

σ0(18)=10+20+30+60+90+180=1+1+1+1+1=6

So when k=0, the sum of divisors (σ0n) function is equal to d(n); i.e., σ0(n) gives the number of divisors of n.

Another example:

σ1(18)=11+21+31+61+91+181=1+2+3+6+9+18=39

when k=1, we actually get the function we expect (a function that sums the divisors).

Important observations

  • if p is a prime number, then σ(p)=1+p since the only divisors of a prime number are 1 and p.
  • if p is a prime number, then σ(pk)=1+p+p2++pk because every power of p is a divisor of pk, e.g., p0,p1,p2,,pk.

Consider

(1)σ(pk)=1+p+p2++pk

Multiplying the expression by p, we have

(2)pσ(pk)=p+p2+p3++pk+1

Subtracting (1) from (2)

pσ(pk)σ(pk)=pk+11

Factoring σ(pk)

σ(pk)(p1)=pk+11

hence

σ(pk)=pk+11p1
  • if p is a product of two distinct primes, say n=pq, then σ(pq)=σ(p)σ(q); also, σ(piqj)=σ(pi)σ(qj)

  • in general, let n=p1a1p2a2pnan, then σ(n)=σ(p1a1)σ(p2a2)σ(pnan) where pi is a prime factor that divides n.

example: σ(18)

σ(18)=σ(322)=σ(32)σ(2)=3313122121=133=39
int sum_of_divisors(int n) {
  int total = 1;
  for (int i = 2; i * i <= n; i += 1) {
    if (n % i == 0) {
      int primePower = i;
      while (n % i == 0) {
        primePower *= i;
        n /= i;
      }
      // sigma(n^k) = (n^{k + 1} - 1) / (k - 1)
      total *= (primePower - 1) / (i  - 1);
    }
  }
  if (n > 1) {
    // if `n` is still a prime number after factorization
    // sigma(n) = 1 + n
    total *= (1 + n);
  }
  return total;
}