Find sum of divisors  | Practice | GeeksforGeeks

Given a natural number N, the task is to find the sum of the divisors of all the divisors of N.
Example 1:
Input:
N = 54
Output:
232
Explanation:
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.
Example 2:
Input:
N = 10
Output:
28
Explanation:
Divisors of 10 are 1, 2, 5, 10
Sums of divisors of all the divisors are 
1, 3, 6, 18.
Overall sum = 1 + 3 + 6 + 18 = 28
Your Task:  
You don't need to read input or print anything. Your task is to complete the function sumOfDivisors() which takes an integer N as an input parameter and return the sum of the divisors of all the divisors of N.
Expected Time Complexity: O(NloglogN)
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 104
0
moaslam8263 weeks ago
c++ soln using seive algorothim
int sumOfDivisors(int N)
    {
         int X=1e4+5;
        vector<int> divsor[X];
        divsor[1].push_back(1);
        for(int i=2;i<X;i++){
            divsor[i].push_back(1);
            for(int j=i;j<X;j+=i){
                divsor[j].push_back(i);
            }
        }
        int cnt=0;
        for(int i=0;i<divsor[N].size();i++){
           for(int j=0;j<divsor[divsor[N][i]].size();j++){
               cnt+=divsor[divsor[N][i]][j];
           }
        }
      
        return cnt;
    }
0
ravisharmaaaa1 month ago
#using python3 code

class Solution:
    def sumOfDivisors(self, N):
        sum=0
    	for i in range (1,N+1):
    	    if(N%i==0):
    	        for j in range (1,i+1):
    	            if(i%j==0):
    	                sum+=j
    	return sum

+1
badgujarsachin833 months ago
int sumOfDivisors(int N)
    {
        // Write Your Code here
        int sum=0;
        // Write Your Code here
        for(int i=1;i<=N;i++){
            if(N%i==0){
                for(int j=1;j<=i;j++){
                    if(i%j==0){
                        sum+=j;
                    }
                }
            }
        }
        return sum;
    }
-1
ipsitadas7243 months ago
class Solution{   static int sumOfDivisors(int N){       // code here       int sum=0;
       for(int i=1;i<=N;i++){           for(int j=1;j<=i;j++){           if( i%j==0 && N%i==0 ){               sum+=j;           }       }       }       return sum;   }}
-1
suyogb53003 months ago
int sumOfDivisors(int N)   {       // Write Your Code here       int sum = 0;       for(int i=1;i<=N;i++){           if(N%i==0){               // sum += i;       for(int j=1;j<=i;j++){           if(i%j==0){               sum += j;           }           }       }       }       return sum;          }
0
mohitraj27413 months ago
c++ solution time: 0/1.5 sec
dry run for better understanding.
 
class Solution{public:int help(int N)   { int sum=0;       // Write Your Code here       for(int i=1;i<=sqrt(N);i++)       {           if(N%i==0)           {               if(i!=N/i)               sum=sum+i+N/i;               else               sum=sum+i;           }       }              return sum;   }
   int sumOfDivisors(int N)   { int sum=0;       // Write Your Code here       for(int i=1;i<=sqrt(N);i++)       {           if(N%i==0)           {               if(i!=N/i)               sum=sum+help(i)+help(N/i);               else               sum=sum+help(i);           }       }              return sum;   }};
 
0
uit215224244 months ago
C++ solution for problem:
class Solution{public:   int sumOfDivisors(int N)   {       int sum=0;       int a[N+1]={0};       for(int i=1;i<N+1;i++){                      if(!(N%i))a[i]++;       }       for(int i=1;i<N+1;i++){           if(a[i]) sum+=sumofDiv(i);   }        return sum;   }       int sumofDiv(int N){       int s=0;       for(int i=1;i<=N;i++){                      if(!(N%i)) s+=i;       }       return s;          }                               }; 
0
doremon215 months ago
JAVA SOLUTION
 
class Solution{   static int sumOfDivisors(int N){       // code here       int sum=0;       for(int i=1;i<=N;i++){           if(N%i==0){               for(int j=1;j<=i;j++){                   if(i%j==0){                       sum=sum+j;                   }               }           }       }return sum;   }}
0
proakash2566 months ago
 static int sumOfDivisors(int n){       // code        int sum = 0;       int temp = 0;       for(int i = 1; i <= n; ++i)       {           if((n % i) == 0)           {               for(int j = 1; j <= i; ++j)               {                   if((i % j) == 0)                       temp = temp + j;               }               sum = sum + temp;               temp = 0;           }       }       return sum;   }
-1
pankajsinghrajput6 months ago
 int sumOfDivisors(int N)
    {
        // Write Your Code here
       int sum = 0;
       vector<int>v;
       for(int i=1; i*i<=N; i++)
           {
               if(N%i == 0)
                 {
                   v.push_back(i);
                   if((N/i)!=i)
                    {
                       v.push_back(N/i);
                    }
                 }
           }
       for(auto it:v)
       {
           int n=it;
           int c=0;
           for(int i=1;i*i<=n;i++)
           {
                if(n%i == 0)
                 {
                   c+=i;
                   if((n/i)!=i)
                    {
                       c+=(n/i);
                    }
                 }
           }
           sum+=c;
       }
       return sum;
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.