The sum of divisors#1782

The sum of divisors (Editorial)

Consider the function , which maps a positive integer to the sum of all its positive divisors.

σ(n) = 1 + p
1, p, p^2, ..., p^a

Their sum forms a geometric progression; therefore,

σ(p^a) = 1 + p + p^2 + p^3 + ... + p^a = \frac{p^{a+1} - 1}{p - 1}

The function is multiplicative, that is, for any pair of coprime integers and , the following equality holds:

σ(xy) = σ(x) σ(y)

Let a positive integer have the following prime factorization

n = p_1^{a_1} p_2^{a_2}...p_k^{a_k},

where p_1, p_2, ..., p_k are distinct prime numbers.

Then, using the multiplicativity of and the formula for powers of prime numbers, we obtain:

σ(n) = σ(p_1^{a_1}) \cdot σ(p_2^{a_2}) \cdot ... \cdot σ(p_k^{a_k}) = \frac{p_1^{a_1+1}-1}{p_1-1} \cdot \frac{p_2^{a_2+1}-1}{p_2-1} \cdot ... \cdot \frac{p_k^{a_k+1}-1}{p_k-1}

The above sum can also be written in the following form:

\sigma(n) = \prod_{i=1}^{k} \frac{p_i^{a_i+1} - 1}{p_i - 1} = \prod_{i=1}^{k} \left( 1 + p_i + p_i^2 + \dots + p_i^{a_i} \right) = \prod_{i=1}^{k} \sum_{j=0}^{a_i} p_i^{\,j}.

Example

The number n = 239 is prime, that is, it has exactly two positive divisors: and . By the definition of the sum-of-divisors function, we obtain:

σ(239) = 1 + 239 = 240

Consider the number n = 24. Its prime factorization is:

24 = 2^3 \cdot 3

Since the numbers 2^3 and are coprime, we can apply the multiplicativity of the function :

σ(24) = σ(2^3) \cdot σ(3) = (1 + 2 + 4 + 8) \cdot (1 + 3) = 15 \cdot 4 = 60.

Let's compute the sum of the divisors of the number n = 24 directly. First, list all positive divisors of :

1, 2, 3, 4, 6, 8, 12, 24

Adding them together, we obtain:

σ(24) = 1 + 2 + 3 + 4 + 6 + 8 + 12 + 24

Notice that all divisors can be grouped in the following way:

σ(24) = \\ 1 + 2 + 3 + 4 + 6 + 8 + 12 + 24 = \\ 1 + 2 + 4 + 8 + 3 \cdot (1 + 2 + 4 + 8) = \\ (1 + 2 + 4 + 8) \cdot (1 + 3) = 15 * 4 = 60

Algorithm implementation

Read the input value .

scanf("%lld",&n);

Store the value of in the variable .

res = 1;

Using the variable , iterate over all prime divisors of the number (according to the problem statement, they do not exceed ). For each such divisor, compute its contribution to the sum of divisors of using the formula derived in the problem analysis.

for (i = 2; i <= 1000; i++)
{
  p = i;

Let be a prime divisor of n. Find the largest exponent such that i^a divides .

  while(n % i == 0)
  {
    n /= i; 
    p *= i;
  }

After the loop terminates, we obtain p = i^{a+1}. Then we multiply the current value of the variable by \frac{i^{a + 1} - 1}{i - 1}.

  res *= (p - 1) / (i - 1);
}

Print the answer.

printf("%lld\n",res);

Java implementation

import java.util.*;

class Main
{
  public static void main(String[] args)
  {
    Scanner con = new Scanner(System.in);
    long n = con.nextLong();
    long res = 1;
    for(int i = 2; i <= 1000; i++)
    {
      long p = i;
      while(n % i == 0)
      {
        n /= i; 
        p *= i;
      }
      res *= (p - 1) / (i - 1);
    }

    System.out.println(res);
    con.close();
  }
}

Python implementation

Read the input value .

n = int(input())

Store the value of in the variable .

res = 1

Using the variable , iterate over all prime divisors of the number (according to the problem statement, they do not exceed ). For each such divisor, compute its contribution to the sum of divisors of using the formula derived in the problem analysis.

for i in range (2,1000) :
  p = i

Let be a prime divisor of n. Find the largest exponent such that i^a divides .

  while(n % i == 0) :
    n = n // i 
    p *= i

After the loop terminates, we obtain p = i^{a+1}. Then we multiply the current value of the variable by \frac{i^{a + 1} - 1}{i - 1}.

  res *= (p - 1) // (i - 1) 

Print the answer.

print(res)