# Find sum of divisors of all the divisors of a natural number

Given a natural number **n**, the task is to find sum of divisors of all the divisors of n.

**Examples:**

Input :n = 54Output :232 Divisors of 54 = 1, 2, 3, 6, 9, 18, 27, 54. Sum of divisors of 1, 2, 3, 6, 9, 18, 27, 54 are 1, 3, 4, 12, 13, 39, 40, 120 respectively. Sum of divisors of all the divisors of 54 = 1 + 3 + 4 + 12 + 13 + 39 + 40 + 120 = 232.Input :n = 10Output :28 Divisors of 10 are 1, 2, 5, 10 Sums of divisors of divisors are 1, 3, 6, 18. Overall sum = 1 + 3 + 6 + 18 = 28

Using the fact that any number **n** can be expressed as product of prime factors, **n** = p_{1}^{k1} x p_{2}^{k2} x … where p_{1}, p_{2}, … are prime numbers.

All the divisors of n can be expressed as p_{1}^{a} x p_{2}^{b} x …, where 0 <= a <= k1 and 0 <= b <= k2.

Now sum of divisors will be sum of all power of p_{1} – p_{1}^{0}, p_{1}^{1},…., p_{1}^{k1} multiplied by all power of p_{2} – p_{2}^{0}, p_{2}^{1},…., p_{2}^{k1}

Sum of Divisor of n

= (p_{1}^{0} x p_{2}^{0}) + (p_{1}^{1} x p_{2}^{0}) +…..+ (p_{1}^{k1} x p_{2}^{0}) +….+ (p_{1}^{0} x p_{2}^{1}) + (p_{1}^{1} x p_{2}^{1}) +…..+ (p_{1}^{k1} x p_{2}^{1}) +……..+

(p_{1}^{0} x p_{2}^{k2}) + (p_{1}^{1} x p_{2}^{k2}) +……+ (p_{1}^{k1} x p_{2}^{k2}).

= (p_{1}^{0} + p_{1}^{1} +…+ p_{1}^{k1}) x p_{2}^{0} + (p_{1}^{0} + p_{1}^{1} +…+ p_{1}^{k1}) x p_{2}^{1} +…….+ (p_{1}^{0} + p_{1}^{1} +…+ p_{1}^{k1}) x p_{2}^{k2}.

= (p_{1}^{0} + p_{1}^{1} +…+ p_{1}^{k1}) x (p_{2}^{0} + p_{2}^{1} +…+ p_{2}^{k2}).

Now, the divisors of any p^{a}, for p as prime, are p^{0}, p^{1},……, p^{a}. And sum of diviors will be (p^{(a+1)} – 1)/(p -1), let it define by f(p).

So, sum of divisors of all divisor will be,

= (f(p_{1}^{0}) + f(p_{1}^{1}) +…+ f(p_{1}^{k1})) x (f(p_{2}^{0}) + f(p_{2}^{1}) +…+ f(p_{2}^{k2})).

So, given a number n, by prime factorization we can find the sum of divisors of all the divisors. But in this problem we are given that n is product of element of array. So, find prime factorization of each element and by using the fact a^{b} x a^{c} = a^{b+c}.

Below is the the implementation of this approach:

## C++

`// C++ program to find sum of divisors of all ` `// the divisors of a natural number. ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Returns sum of divisors of all the divisors ` `// of n ` `int` `sumDivisorsOfDivisors(` `int` `n) ` `{ ` ` ` `// Calculating powers of prime factors and ` ` ` `// storing them in a map mp[]. ` ` ` `map<` `int` `, ` `int` `> mp; ` ` ` `for` `(` `int` `j=2; j<=` `sqrt` `(n); j++) ` ` ` `{ ` ` ` `int` `count = 0; ` ` ` `while` `(n%j == 0) ` ` ` `{ ` ` ` `n /= j; ` ` ` `count++; ` ` ` `} ` ` ` ` ` `if` `(count) ` ` ` `mp[j] = count; ` ` ` `} ` ` ` ` ` `// If n is a prime number ` ` ` `if` `(n != 1) ` ` ` `mp[n] = 1; ` ` ` ` ` `// For each prime factor, calculating (p^(a+1)-1)/(p-1) ` ` ` `// and adding it to answer. ` ` ` `int` `ans = 1; ` ` ` `for` `(` `auto` `it : mp) ` ` ` `{ ` ` ` `int` `pw = 1; ` ` ` `int` `sum = 0; ` ` ` ` ` `for` `(` `int` `i=it.second+1; i>=1; i--) ` ` ` `{ ` ` ` `sum += (i*pw); ` ` ` `pw *= it.first; ` ` ` `} ` ` ` `ans *= sum; ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driven Program ` `int` `main() ` `{ ` ` ` `int` `n = 10; ` ` ` `cout << sumDivisorsOfDivisors(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find sum of divisors ` `# of all the divisors of a natural number. ` `import` `math as mt ` ` ` `# Returns sum of divisors of all ` `# the divisors of n ` `def` `sumDivisorsOfDivisors(n): ` ` ` ` ` `# Calculating powers of prime factors ` ` ` `# and storing them in a map mp[]. ` ` ` `mp ` `=` `dict` `() ` ` ` `for` `j ` `in` `range` `(` `2` `, mt.ceil(mt.sqrt(n))): ` ` ` ` ` `count ` `=` `0` ` ` `while` `(n ` `%` `j ` `=` `=` `0` `): ` ` ` `n ` `/` `/` `=` `j ` ` ` `count ` `+` `=` `1` ` ` ` ` `if` `(count): ` ` ` `mp[j] ` `=` `count ` ` ` ` ` `# If n is a prime number ` ` ` `if` `(n !` `=` `1` `): ` ` ` `mp[n] ` `=` `1` ` ` ` ` `# For each prime factor, calculating ` ` ` `# (p^(a+1)-1)/(p-1) and adding it to answer. ` ` ` `ans ` `=` `1` ` ` `for` `it ` `in` `mp: ` ` ` `pw ` `=` `1` ` ` `summ ` `=` `0` ` ` ` ` `for` `i ` `in` `range` `(mp[it] ` `+` `1` `, ` `0` `, ` `-` `1` `): ` ` ` `summ ` `+` `=` `(i ` `*` `pw) ` ` ` `pw ` `*` `=` `it ` ` ` ` ` `ans ` `*` `=` `summ ` ` ` ` ` `return` `ans ` ` ` `# Driver Code ` `n ` `=` `10` `print` `(sumDivisorsOfDivisors(n)) ` ` ` `# This code is contributed ` `# by mohit kumar 29 ` |

*chevron_right*

*filter_none*

**Output:**

28

**Optimizations : **

For the cases when there are multiple inputs for which we need find the value, we can use Sieve of Eratosthenes as discussed in this post.

This article is contributed by **Anuj Chauhan**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Find all divisors of a natural number | Set 1
- Find all divisors of a natural number | Set 2
- Sum of all proper divisors of a natural number
- Find the sum of the number of divisors
- Find number from its divisors
- Sum of all proper divisors of natural numbers in an array
- Find the number of divisors of all numbers in the range [1, n]
- Program to find count of numbers having odd number of divisors in given range
- Find the largest good number in the divisors of given number N
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors
- Divisors of n-square that are not divisors of n
- Find A and B from list of divisors
- Find numbers with n-divisors in a given range
- Find largest sum of digits in all divisors of n
- Find numbers with K odd divisors in a given range