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 = 3Output:11Explanation:

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 = 4Output:23Explanation:

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.

**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****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++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `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` `frst = 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 - frst) / 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);` `}` |

*chevron_right*

*filter_none*

## 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` `frst = 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 - frst) / 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` |

*chevron_right*

*filter_none*

## 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` ` ` `frst ` `=` `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 ` `-` `frst) ` `/` `/` `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` |

*chevron_right*

*filter_none*

## 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` `frst = 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 - frst) / 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` |

*chevron_right*

*filter_none*

**Output:**

11

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count of integers up to N which are non divisors and non coprime with N
- Count number of integers less than or equal to N which has exactly 9 divisors
- Count of pairs upto N such whose LCM is not equal to their product for Q queries
- Count of divisors of product of an Array in range L to R for Q queries
- Count of subsequences whose product is a difference of square of two integers
- Check if count of even divisors of N is equal to count of odd divisors
- Sum of integers upto N with given unit digit (Set 2)
- Sum of integers upto N with given unit digit
- Count of all values of N in [L, R] such that count of primes upto N is also prime
- Count numbers < = N whose difference with the count of primes upto them is > = K
- Sum of product of all subsets formed by only divisors of N
- Number of divisors of product of N numbers
- Check if a number can be expressed as a product of exactly K prime divisors
- Sum of product of proper divisors of all Numbers lying in range [L, R]
- Product of divisors of a number from a given list of its prime factors
- Maximum GCD of N integers with given product
- Maximum possible GCD for a pair of integers with product N
- Find N integers with given difference between product and sum
- Count of interesting primes upto N
- Find distinct integers for a triplet with given product

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.