Sum of product of all integers upto N with their count of divisors
Given a positive integer N, the task is to find the sum of the product of all the integers in the range [1, N] with their count of divisors.
Examples:
Input: N = 3
Output: 11
Explanation:
Number of positive divisors of 1 is 1( i.e. 1 itself). Therefore, f(1) = 1.
Number of positive divisors of 2 is 2( i.e. 1, 2). Therefore, f(2) = 2.
Number of positive divisors of 3 is 2( i.e. 1, 3). Therefore, f(3) = 2.
So the answer is 1*f(1) + 2*f(2) + 3*f(3) = 1*1 + 2*2 + 3*2 = 11.
Input: N = 4
Output: 23
Explanation:
Here f(1) = 1, f(2) = 2, f(3) = 2 and f(4) = 3. So, the answer is 1*1 + 2*2 + 3*2 + 4*3 = 23.
Naive Approach: The naive approach is to traverse from 1 to N and find the sum of all the numbers with their count of divisors.
Step by step algorithm:
- Initialize a variable ‘ans’ to 0.
- Traverse through every number ‘i’ between 1 and N.
- For each ‘i’, find the first multiple of ‘i’ between 1 and N, and store it in ‘first’.
- Find the last multiple of ‘i’ between 1 and N, and store it in ‘last’.
- Find the total count of multiples of ‘i’ in [1, N], and store it in ‘factors’ using the formula ((last – first) / i) + 1.
- Add ‘totalContribution’ to ‘ans’.
- Return ‘ans’ as the result.
C++
#include <iostream>
#include <cmath>
using namespace std;
long long sumOfProduct( int N) {
long long sum = 0;
for ( int i = 1; i <= N; i++) {
long long factors = 0;
for ( int j = 1; j <= sqrt (i); j++) {
if (i % j == 0) {
factors++;
if (i / j != j) {
factors++;
}
}
}
sum += (i * factors);
}
return sum;
}
int main() {
int N = 3;
cout << sumOfProduct(N) << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static long sumOfProduct( int N) {
long sum = 0 ;
for ( int i = 1 ; i <= N; i++) {
long factors = 0 ;
for ( int j = 1 ; j * j <= i; j++) {
if (i % j == 0 ) {
factors++;
if (i / j != j) {
factors++;
}
}
}
sum += (i * factors);
}
return sum;
}
public static void main(String[] args) {
int N = 3 ;
System.out.println(sumOfProduct(N));
}
}
|
Python3
import math
def sum_of_product(N):
sum_result = 0
for i in range ( 1 , N + 1 ):
factors = 0
for j in range ( 1 , int (math.sqrt(i)) + 1 ):
if i % j = = 0 :
factors + = 1
if i / / j ! = j:
factors + = 1
sum_result + = i * factors
return sum_result
def main():
N = 3
print (sum_of_product(N))
if __name__ = = "__main__" :
main()
|
C#
using System;
class Program
{
static long SumOfProduct( int N)
{
long sum = 0;
for ( int i = 1; i <= N; i++)
{
long factors = 0;
for ( int j = 1; j * j <= i; j++)
{
if (i % j == 0)
{
factors++;
if (i / j != j)
{
factors++;
}
}
}
sum += (i * factors);
}
return sum;
}
static void Main( string [] args)
{
int N = 3;
Console.WriteLine(SumOfProduct(N));
}
}
/// this code is contributed by utkarsh
|
Javascript
function sumOfProduct(N) {
let sum = 0;
for (let i = 1; i <= N; i++) {
let factors = 0;
for (let j = 1; j <= Math.sqrt(i); j++) {
if (i % j === 0) {
factors++;
if (i / j !== j) {
factors++;
}
}
}
sum += (i * factors);
}
return sum;
}
let N = 3;
console.log(sumOfProduct(N));
|
Time Complexity: O(N*sqrt(N))
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to consider the total contribution each number makes to the answer. Below are the steps:
- For any number X in the range [1, N], X contributes K to the sum for each K from 1 to N such that K is a multiple of X.
- Observe that the list of these K is of form i, 2i, 3i, …, Fi where F is the number of multiples of i between 1 and N.
- Hence, to find the sum of these numbers generated above is given by
i*(1+2+3 + … F) = i*(F*(F+1))/2.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long long sumOfFactors( int N)
{
long long ans = 0;
for ( int i = 1; i <= N; i++) {
long long first = i;
long long last = (N / i) * i;
long long factors
= (last - first) / i + 1;
long long totalContribution
= (((factors) * (factors + 1)) / 2) * i;
ans += totalContribution;
}
return ans;
}
int main()
{
int N = 3;
cout << sumOfFactors(N);
}
|
Java
class GFG{
static int sumOfFactors( int N)
{
int ans = 0 ;
for ( int i = 1 ; i <= N; i++)
{
int first = i;
int last = (N / i) * i;
int factors = (last - first) / i + 1 ;
int totalContribution = (((factors) *
(factors + 1 )) / 2 ) * i;
ans += totalContribution;
}
return ans;
}
public static void main(String[] args)
{
int N = 3 ;
System.out.println(sumOfFactors(N));
}
}
|
Python3
def sumOfFactors(N):
ans = 0
for i in range ( 1 , N + 1 ):
first = i
last = (N / / i) * i
factors = (last - first) / / i + 1
totalContribution = (((factors *
(factors + 1 )) / / 2 ) * i)
ans + = totalContribution
return ans
N = 3
print (sumOfFactors(N))
|
C#
using System;
class GFG{
static int sumOfFactors( int N)
{
int ans = 0;
for ( int i = 1; i <= N; i++)
{
int first = i;
int last = (N / i) * i;
int factors = (last - first) / i + 1;
int totalContribution = (((factors) *
(factors + 1)) / 2) * i;
ans += totalContribution;
}
return ans;
}
public static void Main(String[] args)
{
int N = 3;
Console.WriteLine(sumOfFactors(N));
}
}
|
Javascript
<script>
function sumOfFactors(N)
{
var ans = 0;
for (i = 1; i <= N; i++)
{
var first = i;
var last = parseInt(N / i) * i;
var factors = parseInt((last - first) / i )+ 1;
var totalContribution = parseInt(((factors) * (factors + 1)) / 2) * i;
ans += totalContribution;
}
return ans;
}
var N = 3;
document.write(sumOfFactors(N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
27 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...