Sum of all divisors from 1 to N | Set 3
Last Updated :
04 Mar, 2022
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++
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define m 1000000007
void solve( long long n)
{
long long s = 0;
for ( int l = 1; l <= n;) {
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);
s = (s + (((x - y) % m) * p) % m
+ m)
% m;
s %= m;
l = r + 1;
}
cout << (s + m) % m;
}
signed main()
{
long long n = 12;
solve(n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static final int m = 1000000007 ;
static void solve( long n)
{
long s = 0 ;
for ( int l = 1 ; l <= n;)
{
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);
s = (s + (((x - y) %
m) * p) %
m + m) % m;
s %= m;
l = r + 1 ;
}
System.out.print((s + m) % m);
}
public static void main(String[] args)
{
long n = 12 ;
solve(n);
}
}
|
Python3
import math
m = 1000000007
def solve(n):
s = 0 ;
l = 1 ;
while (l < n + 1 ):
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);
s = ((s + (((x - y) % m) *
p) % m + m) % m);
s % = m;
l = r + 1 ;
print ( int ((s + m) % m));
if __name__ = = '__main__' :
n = 12 ;
solve(n);
|
C#
using System;
class GFG{
static readonly int m = 1000000007;
static void solve( long n)
{
long s = 0;
for ( int l = 1; l <= n;)
{
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);
s = (s + (((x - y) %
m) * p) %
m + m) % m;
s %= m;
l = r + 1;
}
Console.Write((s + m) % m);
}
public static void Main(String[] args)
{
long n = 12;
solve(n);
}
}
|
Javascript
<script>
let m = 1000000007;
function solve(n)
{
let s = 0;
for (let l = 1; l <= n;)
{
let r = (n /
Math.floor(n / l));
let x = Math.floor(((r % m) *
((r + 1) %
m)) / 2) % m;
let y = Math.floor(((l % m) *
((l - 1) %
m)) / 2) % m;
let p = (Math.floor(n / l) % m);
s = (s + (((x - y) %
m) * p) %
m + m) % m;
s %= m;
l = r + 1;
}
document.write((s + m) % m);
}
let n = 12;
solve(n);
</script>
|
Time Complexity: O(?N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...