Sum of all divisors from 1 to N | Set 3

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

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive and Linear Approach: Refer to Sum of all divisors from 1 to n for the naive and linear approaches.

Logarithmic Approach: Refer to 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 ` `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; ` `} `

Output:

```127
```

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

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.