Open In App

Sum of product of x and y such that floor(n/x) = y

Last Updated : 12 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a positive integer n. The task is to find the sum of product of x and y such that ?n/x? = y (Integer Division).

Examples: 

Input : n = 5
Output : 21
Following are the possible pairs of (x, y):
(1, 5), (2, 2), (3, 1), (4, 1), (5, 1).
So, 1*5 + 2*2 + 3*1 + 4*1 + 5*1 
   = 5 + 4 + 3 + 4 + 5 
   = 21.

Input : n = 10
Output : 87

Method 1 (Brute Force): 
Iterate x from 1 to n to find y. Then add x*y to the answer over each iteration.

Below is the implementation of this approach :  

C++




// C++ program to find sum of product of x and y
// such that n/x = y (Integer Division)
#include<bits/stdc++.h>
using namespace std;
  
// Return the sum of product x*y.
int sumofproduct(int n)
{
    int ans = 0;
  
    // Iterating x from 1 to n
    for (int x = 1; x <= n; x++)
    {
        // Finding y = n/x.
        int y = n/x;
  
        // Adding product of x and y to answer.
        ans += (y * x);
    }
  
    return ans;
}
  
// Driven Program
int main()
{
    int n = 10;
    cout << sumofproduct(n) << endl;
    return 0;
}


Java




// Java program to find sum of 
// product of x and y such that
// n/x = y (Integer Division) 
import java.io.*;
  
class GFG {
      
// Return the sum of product x*y.
static int sumofproduct(int n)
{
    int ans = 0;
  
    // Iterating x from 1 to n
    for (int x = 1; x <= n; x++)
    {
        // Finding y = n/x.
        int y = n / x;
  
        // Adding product of x and 
        // y to answer.
        ans += (y * x);
    }
  
    return ans;
}
  
    // Driver Code
    static public void main(String[] args)
    {
        int n = 10;
        System.out.println(sumofproduct(n));
    }
}
  
// This code is contributed by vt_m.


Python3




# Python3 program to find sum of
# product of x and y such that 
# n/x = y (Integer Division)
  
# Return the sum of product x*y
def sumofproduct(n):
    ans = 0
  
    # Iterating x from 1 to n
    for x in range(1, n + 1):
          
        # Finding y = n/x.
        y = int(n / x)
  
        # Adding product of x and y to answer.
        ans += (y * x)
  
    return ans
  
# Driven Program
n = 10
print (sumofproduct(n))
  
#This code is Shreyanshi Arun


C#




// C# program to find sum of 
// product of x and y such that
// n/x = y (Integer Division) 
using System;
  
class GFG {
      
// Return the sum of product x*y.
static int sumofproduct(int n)
{
    int ans = 0;
  
    // Iterating x from 1 to n
    for (int x = 1; x <= n; x++)
    {
        // Finding y = n/x.
        int y = n / x;
  
        // Adding product of x and 
        // y to answer.
        ans += (y * x);
    }
  
    return ans;
}
  
    // Driver Code
    static public void Main(String[] args)
    {
        int n = 10;
        Console.WriteLine(sumofproduct(n));
    }
}
  
// This code is contributed by vt_m.


PHP




<?php
// PHP program to find sum of product
// of x and y such that n/x = y 
// (Integer Division)
  
// Return the sum of product x*y.
function sumofproduct($n)
{
    $ans = 0;
  
    // Iterating x from 1 to n
    for($x = 1; $x <= $n; $x++)
    {
        // Finding y = n/x.
        $y = (int)($n / $x);
  
        // Adding product of x 
        // and y to answer.
        $ans += ($y * $x);
    }
  
    return $ans;
}
  
// Driver Code
$n = 10;
echo sumofproduct($n);
  
// This code is contributed by mits
?>


Javascript




<script>
  
// Javascript program to find sum of 
// product of x and y such that
// n/x = y (Integer Division) 
     
// Return the sum of product x*y.
function sumofproduct(n)
{
    var ans = 0;
  
    // Iterating x from 1 to n
    for(x = 1; x <= n; x++)
    {
          
        // Finding y = n/x.
        var y = parseInt(n / x);
  
        // Adding product of x and 
        // y to answer.
        ans += (y * x);
    }
    return ans;
}
  
// Driver Code
var n = 10;
document.write(sumofproduct(n));
  
// This code is contributed by Princi Singh 
  
</script>


Output :

87

Time Complexity : O(n)
Method 2 (Efficient Approach): 
Let’s solve for n = 10, so 
x = 1, y = 10 
x = 2, y = 5 
x = 3, y = 3 
x = 4, y = 2 
x = 5, y = 2 
x = 6, y = 1 
x = 7, y = 1 
x = 8, y = 1 
x = 9, y = 1 
x = 10, y = 1
So, our answer would be 1*10 + 2*5 + 3*3 + 4*2 + 5*2 + 6*1 + 7*1 + 8*1 + 9*1 + 10*1.
Now, observe some value of y is repeating. Also, observe that they are repeating for some range of consecutive value of x like y = 1 is repeating for x = 6 to 10.

So, instead of finding the value of y for all the value of x (1 to n) as done in method 1, try to find the lower and higher value of x for which the value of possible value of y like for y = 1 try to find lower value of x = 6 and higher value of x = 10. Now, observe lower value will be (n/(y+1)) + 1 and higher value will be (n/y). Find the sum of range of x and multiply with y and add to the answer.
How to find the possible value of y? 

Observe, y has all values from 1 to sqrt(n) when y is smaller than or equal to x. So for y = 1 to sqrt(n), find the lower and higher limits of x for each y. For n = 10, 
y = 1, lo = 6 and hi = 10, ans += (6 + 7 + 8 + 9 + 10)*1 
y = 2, lo = 4 and hi = 5, ans += (4 + 5)*2 
y = 3, lo = 3 and hi = 3, ans += (3)*3

For other values to be added (for y = 10 and 5 in n = 10), observe they can be found in above steps, for each y, add y * (n/y) in the answer. 
For n = 10, 
y = 1, ans += 1 * (10/1) 
y = 2, ans += 2 * (10/2).

Below is the implementation of this approach:  

C++




// C++ program to find sum of product of x and y
// such that n/x = y (Integer Division)
#include<bits/stdc++.h>
using namespace std;
  
// Return the sum of natural number in a range.
int sumOfRange(int a, int b)
{
    // n*(n+1)/2.
    int i = (a * (a+1)) >> 1;
    int j = (b * (b+1)) >> 1;
    return (i - j);
}
  
// Return the sum of product x*y.
int sumofproduct(int n)
{
    int sum = 0;
  
    // Iterating i from 1 to sqrt(n)
    int root = sqrt(n);
    for (int i=1; i<=root; i++)
    {
        // Finding the upper limit.
        int up = n/i;
  
        // Finding the lower limit.
        int low = max(n/(i+1), root);
  
        sum += (i * sumOfRange(up, low));
        sum += (i * (n/i));
    }
  
    return sum;
}
  
// Driven Program
int main()
{
    int n = 10;
    cout << sumofproduct(n) << endl;
    return 0;
}


Java




// Java program to find sum of 
// product of x and y such that
// n / x = y (Integer Division)
import java.io.*;
  
class GFG {
      
// Return the sum of natural number in a range.
static int sumOfRange(int a, int b)
{
    // n * (n + 1) / 2.
    int i = (a * (a + 1)) >> 1;
    int j = (b * (b + 1)) >> 1;
    return (i - j);
}
  
// Return the sum of product x*y.
static int sumofproduct(int n)
{
    int sum = 0;
  
    // Iterating i from 1 to sqrt(n)
    int root = (int)Math.sqrt(n);
    for (int i = 1; i <= root; i++)
    {
        // Finding the upper limit.
        int up = n / i;
  
        // Finding the lower limit.
        int low = Math.max(n / (i + 1), root);
  
        sum += (i * sumOfRange(up, low));
        sum += (i * (n / i));
    }
  
    return sum;
}
  
    // Driver Code
    static public void main(String[] args)
    {
        int n = 10;
        System.out.println(sumofproduct(n));
    }
}
  
// This code is contributed by vt_m.


Python3




# Python3 program to find sum 
# of product of x and y such 
# that n/x = y (Integer Division)
import math
  
# Return the sum of natural 
# number in a range.
def sumOfRange(a, b):
    # n*(n+1)/2.
    i = (a * (a + 1)) >> 1;
    j = (b * (b + 1)) >> 1;
    return (i - j);
  
# Return the sum of product x*y.
def sumofproduct(n):
    sum = 0;
  
    # Iterating i from 1 to sqrt(n)
    root = int(math.sqrt(n));
    for i in range(1, root + 1):
        # Finding the upper limit.
        up = int(n / i);
  
        # Finding the lower limit.
        low = max(int(n / (i + 1)), root);
  
        sum += (i * sumOfRange(up, low));
        sum += (i * int(n / i));
  
    return sum;
  
# Driven Code
n = 10;
print(sumofproduct(n));
      
# This code is contributed by mits


C#




// C# program to find sum of 
// product of x and y such that
// n / x = y (Integer Division)
using System;
  
class GFG {
      
// Return the sum of natural number in a range.
static int sumOfRange(int a, int b)
{
    // n * (n + 1) / 2.
    int i = (a * (a + 1)) >> 1;
    int j = (b * (b + 1)) >> 1;
    return (i - j);
}
  
// Return the sum of product x*y.
static int sumofproduct(int n)
{
    int sum = 0;
  
    // Iterating i from 1 to sqrt(n)
    int root = (int)Math.Sqrt(n);
    for (int i = 1; i <= root; i++)
    {
        // Finding the upper limit.
        int up = n / i;
  
        // Finding the lower limit.
        int low = Math.Max(n / (i + 1), root);
  
        sum += (i * sumOfRange(up, low));
        sum += (i * (n / i));
    }
  
    return sum;
}
  
    // Driver Code
    static public void Main(String[] args)
    {
        int n = 10;
        Console.WriteLine(sumofproduct(n));
    }
}
  
// This code is contributed by vt_m.


PHP




<?php
// PHP program to find sum of 
// product of x and y such 
// that n/x = y (Integer Division)
  
// Return the sum of natural 
// number in a range.
function sumOfRange($a, $b)
{
    // n*(n+1)/2.
    $i = ($a * ($a + 1)) >> 1;
    $j = ($b * ($b + 1)) >> 1;
    return ($i - $j);
}
  
// Return the sum of product x*y.
function sumofproduct($n)
{
    $sum = 0;
  
    // Iterating i from 1 to sqrt(n)
    $root = sqrt($n);
    for ($i = 1; $i <= $root; $i++)
    {
        // Finding the upper limit.
        $up = (int)($n / $i);
  
        // Finding the lower limit.
        $low = max((int)($n / ($i + 1)), $root);
  
        $sum += ($i * sumOfRange($up, $low));
        $sum += ($i * (int)($n / $i));
    }
  
    return $sum;
}
  
// Driven Code
$n = 10;
echo sumofproduct($n) . "\n";
  
// This code is contributed 
// by Akanksha Rai(Abby_akku)
?>


Javascript




<script>
  
// Javascript program to implement
// the above approach
  
// Return the sum of natural number in a range.
function sumOfRange(a, b)
{
    // n * (n + 1) / 2.
    let i = (a * (a + 1)) >> 1;
    let j = (b * (b + 1)) >> 1;
    return (i - j);
}
   
// Return the sum of product x*y.
function sumofproduct(n)
{
    let sum = 0;
   
    // Iterating i from 1 to sqrt(n)
    let root = Math.floor(Math.sqrt(n));
    for (let i = 1; i <= root; i++)
    {
        // Finding the upper limit.
        let up = Math.floor(n / i);
   
        // Finding the lower limit.
        let low = Math.max(Math.floor(n / (i + 1)), root);
   
        sum += (i * sumOfRange(up, low));
        sum += (i * Math.floor(n / i));
    }
   
    return sum;
  
    // Driver Code
          
    let n = 10;
    document.write(sumofproduct(n));
          
</script>


Output: 

87

Time Complexity: O((?n)
Source: 
https://www.quora.com/What-is-the-fastest-way-to-solve-the-problem-that-states-given-a-number-N-find-the-sum-of-all-products-x*y-such-that-N-x-y-integer-division-Here-N-would-be-N-10

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads