Given a natural number n, the task is to find sum of divisors of all the divisors of n.
Input : n = 54 Output : 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 = 10 Output : 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 = p1k1 x p2k2 x … where p1, p2, … are prime numbers.
All the divisors of n can be expressed as p1a x p2b x …, where 0 <= a <= k1 and 0 <= b <= k2.
Now sum of divisors will be sum of all power of p1 – p10, p11,…., p1k1 multiplied by all power of p2 – p20, p21,…., p2k1
Sum of Divisor of n
= (p10 x p20) + (p11 x p20) +…..+ (p1k1 x p20) +….+ (p10 x p21) + (p11 x p21) +…..+ (p1k1 x p21) +……..+
(p10 x p2k2) + (p11 x p2k2) +……+ (p1k1 x p2k2).
= (p10 + p11 +…+ p1k1) x p20 + (p10 + p11 +…+ p1k1) x p21 +…….+ (p10 + p11 +…+ p1k1) x p2k2.
= (p10 + p11 +…+ p1k1) x (p20 + p21 +…+ p2k2).
Now, the divisors of any pa, for p as prime, are p0, p1,……, pa. 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(p10) + f(p11) +…+ f(p1k1)) x (f(p20) + f(p21) +…+ f(p2k2)).
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 ab x ac = ab+c.
Below is the implementation of this approach:
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 email@example.com. 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find sum of inverse of the divisors when sum of divisors and the number is given
- Sum of all proper divisors of a natural number
- Find all divisors of a natural number | Set 2
- Find all divisors of a natural number | Set 1
- Sum of all proper divisors of natural numbers in an array
- Find all divisors of first N natural numbers
- Check if a number has an odd count of odd divisors and even count of even divisors
- Sum of sum of all subsets of a set formed by first N natural numbers
- Sum of all prime divisors of all the numbers in range L-R
- Find all Factors of Large Perfect Square Natural Number in O(sqrt(sqrt(N))
- Check if a given number can be expressed as pair-sum of sum of first X natural numbers
- Divisors of n-square that are not divisors of n
- Maximum possible prime divisors that can exist in numbers having exactly N divisors
- Check if count of even divisors of N is equal to count of odd divisors
- Find sum of modulo K of first N natural number
- Find if given number is sum of first n natural numbers
- Find largest sum of digits in all divisors of n
- Sum of all the prime divisors of a number
- Break a number such that sum of maximum divisors of all parts is minimum
- Sum of all second largest divisors after splitting a number into one or more parts