Given a natural number n, the task is to find sum of divisors of all the divisors of n.
Examples:
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 divisors 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:
C++
#include<bits/stdc++.h>
using namespace std;
int sumDivisorsOfDivisors( int n)
{
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 != 1)
mp[n] = 1;
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;
}
int main()
{
int n = 10;
cout << sumDivisorsOfDivisors(n);
return 0;
}
|
Java
import java.util.HashMap;
class GFG
{
public static int sumDivisorsOfDivisors( int n)
{
HashMap<Integer, Integer> mp = new HashMap<>();
for ( int j = 2 ; j <= Math.sqrt(n); j++)
{
int count = 0 ;
while (n % j == 0 )
{
n /= j;
count++;
}
if (count != 0 )
mp.put(j, count);
}
if (n != 1 )
mp.put(n, 1 );
int ans = 1 ;
for (HashMap.Entry<Integer, Integer> entry : mp.entrySet())
{
int pw = 1 ;
int sum = 0 ;
for ( int i = entry.getValue() + 1 ; i >= 1 ; i--)
{
sum += (i * pw);
pw *= entry.getKey();
}
ans *= sum;
}
return ans;
}
public static void main(String[] args)
{
int n = 10 ;
System.out.println(sumDivisorsOfDivisors(n));
}
}
|
Python3
import math as mt
def sumDivisorsOfDivisors(n):
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 ! = 1 ):
mp[n] = 1
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
n = 10
print (sumDivisorsOfDivisors(n))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static int sumDivisorsOfDivisors( int n)
{
Dictionary< int ,
int > mp = new Dictionary< int ,
int >();
for ( int j = 2; j <= Math.Sqrt(n); j++)
{
int count = 0;
while (n % j == 0)
{
n /= j;
count++;
}
if (count != 0)
mp.Add(j, count);
}
if (n != 1)
mp.Add(n, 1);
int ans = 1;
foreach (KeyValuePair< int , int > entry in mp)
{
int pw = 1;
int sum = 0;
for ( int i = entry.Value + 1;
i >= 1; i--)
{
sum += (i * pw);
pw = entry.Key;
}
ans *= sum;
}
return ans;
}
public static void Main(String[] args)
{
int n = 10;
Console.WriteLine(sumDivisorsOfDivisors(n));
}
}
|
Javascript
<script>
function sumDivisorsOfDivisors(n)
{
let mp = new Map();
for (let j = 2; j <= Math.sqrt(n); j++)
{
let count = 0;
while (n % j == 0)
{
n = Math.floor(n/j);
count++;
}
if (count != 0)
mp.set(j, count);
}
if (n != 1)
mp.set(n, 1);
let ans = 1;
for (let [key, value] of mp.entries())
{
let pw = 1;
let sum = 0;
for (let i = value + 1; i >= 1; i--)
{
sum += (i * pw);
pw = key;
}
ans *= sum;
}
return ans;
}
let n = 10;
document.write(sumDivisorsOfDivisors(n));
</script>
|
Output:
28
Time Complexity: O(√n log n)
Auxiliary Space: O(n)
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 Aarti_Rathi and Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.