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 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
from*x***1**to**N**, occurs in the sum up to it’s highest multiple which is ≤**N**. - Hence, calculate the contribution of each
by the formula*x***x * floor(N / x)**, - It can be observed that
**floor(N/i)**is same for a series of continuous numbers**l**. Hence, instead of calculating_{1}, l_{2}, l_{3}….l_{r}**l**for each_{i}***floor(N/i)****i**, calculate**(l**, thus reducing the computational complexity._{1}+ l_{2}+ l_{3}+….+ l_{r}) * floor(N/l_{1})

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; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

127

**Time Complexity:** O(√N)

**Auxiliary Space:** O(1)

## Recommended Posts:

- Find sum of divisors of all the divisors of a natural number
- Find sum of inverse of the divisors when sum of divisors and the number is given
- 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
- Check if a number has an odd count of odd divisors and even count of even divisors
- Sum of all prime divisors of all the numbers in range L-R
- Sum of all divisors from 1 to N | Set 2
- Sum of all proper divisors of a natural number
- Sum of all proper divisors of natural numbers in an array
- 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 divisors from 1 to n
- Find largest sum of digits in all divisors of n
- Sum of product of all subsets formed by only divisors of N
- Sum of all second largest divisors after splitting a number into one or more parts
- Sum of product of proper divisors of all Numbers lying in range [L, R]
- Sum of all proper divisors from 1 to N
- Sum of all perfect square divisors of numbers from 1 to N
- Count all pairs of divisors of a number N whose sum is coprime with N

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.