 GeeksforGeeks App
Open App Browser
Continue

# 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:

1. Initialize a variable ‘ans’ to 0.
2. Traverse through every number ‘i’ between 1 and N.
3. For each ‘i’, find the first multiple of ‘i’ between 1 and N, and store it in ‘first’.
4. Find the last multiple of ‘i’ between 1 and N, and store it in ‘last’.
5. Find the total count of multiples of ‘i’ in [1, N], and store it in ‘factors’ using the formula ((last – first) / i) + 1.
7. Return ‘ans’ as the result.

## C++

 `#include ``#include ``using` `namespace` `std;` `// Function to find sum of product of integers and their count of divisors``long` `long` `sumOfProduct(``int` `N) {``    ``long` `long` `sum = 0;` `    ``// Iterate from 1 to N``    ``for` `(``int` `i = 1; i <= N; i++) {``        ``long` `long` `factors = 0;` `        ``// Find the count of divisors of i by iterating from 1 to sqrt(i)``        ``for` `(``int` `j = 1; j <= ``sqrt``(i); j++) {``            ``if` `(i % j == 0) {``                ``factors++;``                ``// If the divisors are not the same, count them twice``                ``if` `(i / j != j) {``                    ``factors++;``                ``}``            ``}``        ``}``        ` `        ``// Add the product of i and its count of divisors to the sum``        ``sum += (i * factors);``    ``}` `    ``// Return the final sum``    ``return` `sum;``}` `int` `main() {``    ``int` `N = 3;``    ``cout << sumOfProduct(N) << endl;``    ``return` `0;``}`

Output

`11`

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:

1. 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.
2. 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.
3. 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++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to find the sum of the``// product of all the integers and``// their positive divisors up to N``long` `long` `sumOfFactors(``int` `N)``{``    ``long` `long` `ans = 0;` `    ``// Iterate for every number``    ``// between 1 and N``    ``for` `(``int` `i = 1; i <= N; i++) {` `        ``// Find the first multiple``        ``// of i between 1 and N``        ``long` `long` `first = i;` `        ``// Find the last multiple``        ``// of i between 1 and N``        ``long` `long` `last = (N / i) * i;` `        ``// Find the total count of``        ``// multiple of in [1, N]``        ``long` `long` `factors``            ``= (last - first) / i + 1;` `        ``// Compute the contribution of i``        ``// using the formula``        ``long` `long` `totalContribution``            ``= (((factors) * (factors + 1)) / 2) * i;` `        ``// Add the contribution``        ``// of i to the answer``        ``ans += totalContribution;``    ``}` `    ``// Return the result``    ``return` `ans;``}` `// Driver code``int` `main()``{``    ``// Given N``    ``int` `N = 3;` `    ``// function call``    ``cout << sumOfFactors(N);``}`

## Java

 `// Java program for the above approach``class` `GFG{``  ` `// Function to find the sum of the``// product of all the integers and``// their positive divisors up to N``static` `int` `sumOfFactors(``int` `N)``{``    ``int` `ans = ``0``;`` ` `    ``// Iterate for every number``    ``// between 1 and N``    ``for` `(``int` `i = ``1``; i <= N; i++)``    ``{`` ` `        ``// Find the first multiple``        ``// of i between 1 and N``        ``int` `first = i;`` ` `        ``// Find the last multiple``        ``// of i between 1 and N``        ``int` `last = (N / i) * i;`` ` `        ``// Find the total count of``        ``// multiple of in [1, N]``        ``int` `factors = (last - first) / i + ``1``;`` ` `        ``// Compute the contribution of i``        ``// using the formula``        ``int` `totalContribution = (((factors) *``                                  ``(factors + ``1``)) / ``2``) * i;`` ` `        ``// Add the contribution``        ``// of i to the answer``        ``ans += totalContribution;``    ``}`` ` `    ``// Return the result``    ``return` `ans;``}`` ` `// Driver code``public` `static` `void` `main(String[] args)``{``    ``// Given N``    ``int` `N = ``3``;`` ` `    ``// function call``    ``System.out.println(sumOfFactors(N));``}``}` `// This code is contributed by Ritik Bansal`

## Python3

 `# Python3 implementation of``# the above approach` `# Function to find the sum of the``# product of all the integers and``# their positive divisors up to N``def` `sumOfFactors(N):` `    ``ans ``=` `0` `    ``# Iterate for every number``    ``# between 1 and N``    ``for` `i ``in` `range``(``1``, N ``+` `1``):` `        ``# Find the first multiple``        ``# of i between 1 and N``        ``first ``=` `i` `        ``# Find the last multiple``        ``# of i between 1 and N``        ``last ``=` `(N ``/``/` `i) ``*` `i` `        ``# Find the total count of``        ``# multiple of in [1, N]``        ``factors ``=` `(last ``-` `first) ``/``/` `i ``+` `1` `        ``# Compute the contribution of i``        ``# using the formula``        ``totalContribution ``=` `(((factors ``*``                              ``(factors ``+` `1``)) ``/``/` `2``) ``*` `i)` `        ``# Add the contribution``        ``# of i to the answer``        ``ans ``+``=` `totalContribution` `    ``# Return the result``    ``return` `ans` `# Driver Code` `# Given N``N ``=` `3` `# Function call``print``(sumOfFactors(N))` `# This code is contributed by Shivam Singh`

## C#

 `// C# program for the above approach``using` `System;``class` `GFG{``  ` `// Function to find the sum of the``// product of all the integers and``// their positive divisors up to N``static` `int` `sumOfFactors(``int` `N)``{``    ``int` `ans = 0;`` ` `    ``// Iterate for every number``    ``// between 1 and N``    ``for` `(``int` `i = 1; i <= N; i++)``    ``{`` ` `        ``// Find the first multiple``        ``// of i between 1 and N``        ``int` `first = i;`` ` `        ``// Find the last multiple``        ``// of i between 1 and N``        ``int` `last = (N / i) * i;`` ` `        ``// Find the total count of``        ``// multiple of in [1, N]``        ``int` `factors = (last - first) / i + 1;`` ` `        ``// Compute the contribution of i``        ``// using the formula``        ``int` `totalContribution = (((factors) *``                                  ``(factors + 1)) / 2) * i;`` ` `        ``// Add the contribution``        ``// of i to the answer``        ``ans += totalContribution;``    ``}`` ` `    ``// Return the result``    ``return` `ans;``}`` ` `// Driver code``public` `static` `void` `Main(String[] args)``{``    ``// Given N``    ``int` `N = 3;`` ` `    ``// function call``    ``Console.WriteLine(sumOfFactors(N));``}``}` `// This code is contributed by gauravrajput1`

## Javascript

 ``

Output:

`11`

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

My Personal Notes arrow_drop_up