Skip to content
Related Articles

Related Articles

Sum of all divisors from 1 to N | Set 3
  • Difficulty Level : Hard
  • Last Updated : 29 Oct, 2020
GeeksforGeeks - Summer Carnival Banner

Given a positive integer N, the task is to find the sum of divisors of all the numbers from 1 to N.
Examples: 

Input: N = 5 
Output: 21 
Explanation: 
Sum of divisors of all numbers from 1 to 5 = 21. 
Divisors of 1 -> 1 
Divisors of 2 -> 1, 2 
Divisors of 3 -> 1, 3 
Divisors of 4 -> 1, 2, 4 
Divisors of 5 -> 1, 5, hence Sum = 21

Input: N = 6 
Output: 33 
Explanation: 
Sum of divisors of all numbers from 1 to 6 = 33. 
Divisors of 1 -> 1 
Divisors of 2 -> 1, 2 
Divisors of 3 -> 1, 3 
Divisors of 4 -> 1, 2, 4 
Divisors of 5 -> 1, 5 
Divisors of 6 -> 1, 2, 3, 6, hence sum = 33 

Naive and Linear Approach: Refer to the Sum of all divisors from 1 to n for the naive and linear approaches.
Logarithmic Approach: Refer to the Sum of all divisors from 1 to N | Set 2 for the logarithmic time approach.

Efficient Approach: 
Follow the steps below to solve the problem:  



  • We can observe that for each number x from 1 to N, occurs in the sum up to it’s highest multiple which is ≤ N.
  • Hence, calculate the contribution of each x by the formula x * floor(N / x),
  • It can be observed that floor(N/i) is same for a series of continuous numbers l1, l2, l3….lr. Hence, instead of calculating li * floor(N/i) for each i, calculate (l1 + l2 + l3 +….+ lr) * floor(N/l1), thus reducing the computational complexity.

Below is the implementation of the above approach:

C++




// C++ Program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
#define int long long int
#define m 1000000007
 
// Function to find the sum
// of all divisors of all
// numbers from 1 to N
void solve(long long n)
{
 
    // Stores the sum
    long long s = 0;
 
    for (int l = 1; l <= n;) {
 
        // Marks the last point of
        // occurence with same count
        int r = n / floor(n / l);
 
        int x = (((r % m) * ((r + 1)
                             % m))
                 / 2)
                % m;
        int y = (((l % m) * ((l - 1)
                             % m))
                 / 2)
                % m;
        int p = ((n / l) % m);
 
        // Calculate the sum
        s = (s + (((x - y) % m) * p) % m
             + m)
            % m;
 
        s %= m;
        l = r + 1;
    }
 
    // Return the result
    cout << (s + m) % m;
}
 
// Driver Code
signed main()
{
    long long n = 12;
    solve(n);
    return 0;
}

Java




// Java Program to implement
// the above approach
import java.util.*;
class GFG{
 
static final int m = 1000000007;
 
// Function to find the sum
// of all divisors of all
// numbers from 1 to N
static void solve(long n)
{
  // Stores the sum
  long s = 0;
 
  for (int l = 1; l <= n;)
  {
    // Marks the last point of
    // occurence with same count
    int r = (int)(n /
             Math.floor(n / l));
 
    int x = (((r % m) *
             ((r + 1) %
               m)) / 2) % m;
    int y = (((l % m) *
             ((l - 1) %
               m)) / 2) % m;
    int p = (int)((n / l) % m);
 
    // Calculate the sum
    s = (s + (((x - y) %
                m) * p) %
                m + m) % m;
 
    s %= m;
    l = r + 1;
  }
 
  // Return the result
  System.out.print((s + m) % m);
}
 
// Driver Code
public static void main(String[] args)
{
  long n = 12;
  solve(n);
}
}
 
// This code is contributed by Rajput-Ji

Python3




# Python3 Program to implement
# the above approach
import math
m = 1000000007
 
# Function to find the sum
# of all divisors of all
# numbers from 1 to N
def solve(n):
   
    # Stores the sum
    s = 0;
    l = 1;
    while(l < n + 1):
       
        # Marks the last poof
        # occurence with same count
        r = (int)(n /
             math.floor(n / l));
 
        x = ((((r % m) *
              ((r + 1) % m)) / 2) % m);
        y = ((((l % m) *
              ((l - 1) % m)) / 2) % m);
        p = (int)((n / l) % m);
 
        # Calculate the sum
        s = ((s + (((x - y) % m) *
                     p) % m + m) % m);
 
        s %= m;
        l = r + 1;
 
    # Return the result
    print (int((s + m) % m));
 
# Driver Code
if __name__ == '__main__':
   
    n = 12;
    solve(n);
 
# This code is contributed by Rajput-Ji

C#




// C# program to implement
// the above approach
using System;
 
class GFG{
 
static readonly int m = 1000000007;
 
// Function to find the sum
// of all divisors of all
// numbers from 1 to N
static void solve(long n)
{
   
  // Stores the sum
  long s = 0;
 
  for(int l = 1; l <= n;)
  {
     
    // Marks the last point of
    // occurence with same count
    int r = (int)(n /(Math.Floor((double)n/l)));
 
    int x = (((r % m) *
             ((r + 1) %
             m)) / 2) % m;
    int y = (((l % m) *
             ((l - 1) %
             m)) / 2) % m;
    int p = (int)((n / l) % m);
 
    // Calculate the sum
    s = (s + (((x - y) %
               m) * p) %
                m + m) % m;
 
    s %= m;
    l = r + 1;
  }
   
  // Return the result
  Console.Write((s + m) % m);
}
 
// Driver Code
public static void Main(String[] args)
{
  long n = 12;
   
  solve(n);
}
}
 
// This code is contributed by Amit Katiyar
Output
127

Time Complexity: O(√N) 
Auxiliary Space: O(1)

competitive-programming-img

My Personal Notes arrow_drop_up
Recommended Articles
Page :