Sum of product of all integers upto N with their count of divisors | C ++ program for the above approach ; Function to find the sum of the product of all the integers and their positive divisors up to N ; Iterate for every number between 1 and N ; Find the first multiple of i between 1 and N ; Find the last multiple of i between 1 and N ; Find the total count of multiple of in [ 1 , N ] ; Compute the contribution of i using the formula ; Add the contribution of i to the answer ; Return the result ; Driver code ; Given N ; function call
#include <bits/stdc++.h> NEW_LINE 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 ) ; }