Sum of product of x and y such that floor(n/x) = y
Last Updated :
12 Sep, 2023
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++
#include<bits/stdc++.h>
using namespace std;
int sumofproduct( int n)
{
int ans = 0;
for ( int x = 1; x <= n; x++)
{
int y = n/x;
ans += (y * x);
}
return ans;
}
int main()
{
int n = 10;
cout << sumofproduct(n) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int sumofproduct( int n)
{
int ans = 0 ;
for ( int x = 1 ; x <= n; x++)
{
int y = n / x;
ans += (y * x);
}
return ans;
}
static public void main(String[] args)
{
int n = 10 ;
System.out.println(sumofproduct(n));
}
}
|
Python3
def sumofproduct(n):
ans = 0
for x in range ( 1 , n + 1 ):
y = int (n / x)
ans + = (y * x)
return ans
n = 10
print (sumofproduct(n))
|
C#
using System;
class GFG {
static int sumofproduct( int n)
{
int ans = 0;
for ( int x = 1; x <= n; x++)
{
int y = n / x;
ans += (y * x);
}
return ans;
}
static public void Main(String[] args)
{
int n = 10;
Console.WriteLine(sumofproduct(n));
}
}
|
PHP
<?php
function sumofproduct( $n )
{
$ans = 0;
for ( $x = 1; $x <= $n ; $x ++)
{
$y = (int)( $n / $x );
$ans += ( $y * $x );
}
return $ans ;
}
$n = 10;
echo sumofproduct( $n );
?>
|
Javascript
<script>
function sumofproduct(n)
{
var ans = 0;
for (x = 1; x <= n; x++)
{
var y = parseInt(n / x);
ans += (y * x);
}
return ans;
}
var n = 10;
document.write(sumofproduct(n));
</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++
#include<bits/stdc++.h>
using namespace std;
int sumOfRange( int a, int b)
{
int i = (a * (a+1)) >> 1;
int j = (b * (b+1)) >> 1;
return (i - j);
}
int sumofproduct( int n)
{
int sum = 0;
int root = sqrt (n);
for ( int i=1; i<=root; i++)
{
int up = n/i;
int low = max(n/(i+1), root);
sum += (i * sumOfRange(up, low));
sum += (i * (n/i));
}
return sum;
}
int main()
{
int n = 10;
cout << sumofproduct(n) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int sumOfRange( int a, int b)
{
int i = (a * (a + 1 )) >> 1 ;
int j = (b * (b + 1 )) >> 1 ;
return (i - j);
}
static int sumofproduct( int n)
{
int sum = 0 ;
int root = ( int )Math.sqrt(n);
for ( int i = 1 ; i <= root; i++)
{
int up = n / i;
int low = Math.max(n / (i + 1 ), root);
sum += (i * sumOfRange(up, low));
sum += (i * (n / i));
}
return sum;
}
static public void main(String[] args)
{
int n = 10 ;
System.out.println(sumofproduct(n));
}
}
|
Python3
import math
def sumOfRange(a, b):
i = (a * (a + 1 )) >> 1 ;
j = (b * (b + 1 )) >> 1 ;
return (i - j);
def sumofproduct(n):
sum = 0 ;
root = int (math.sqrt(n));
for i in range ( 1 , root + 1 ):
up = int (n / i);
low = max ( int (n / (i + 1 )), root);
sum + = (i * sumOfRange(up, low));
sum + = (i * int (n / i));
return sum ;
n = 10 ;
print (sumofproduct(n));
|
C#
using System;
class GFG {
static int sumOfRange( int a, int b)
{
int i = (a * (a + 1)) >> 1;
int j = (b * (b + 1)) >> 1;
return (i - j);
}
static int sumofproduct( int n)
{
int sum = 0;
int root = ( int )Math.Sqrt(n);
for ( int i = 1; i <= root; i++)
{
int up = n / i;
int low = Math.Max(n / (i + 1), root);
sum += (i * sumOfRange(up, low));
sum += (i * (n / i));
}
return sum;
}
static public void Main(String[] args)
{
int n = 10;
Console.WriteLine(sumofproduct(n));
}
}
|
PHP
<?php
function sumOfRange( $a , $b )
{
$i = ( $a * ( $a + 1)) >> 1;
$j = ( $b * ( $b + 1)) >> 1;
return ( $i - $j );
}
function sumofproduct( $n )
{
$sum = 0;
$root = sqrt( $n );
for ( $i = 1; $i <= $root ; $i ++)
{
$up = (int)( $n / $i );
$low = max((int)( $n / ( $i + 1)), $root );
$sum += ( $i * sumOfRange( $up , $low ));
$sum += ( $i * (int)( $n / $i ));
}
return $sum ;
}
$n = 10;
echo sumofproduct( $n ) . "\n" ;
?>
|
Javascript
<script>
function sumOfRange(a, b)
{
let i = (a * (a + 1)) >> 1;
let j = (b * (b + 1)) >> 1;
return (i - j);
}
function sumofproduct(n)
{
let sum = 0;
let root = Math.floor(Math.sqrt(n));
for (let i = 1; i <= root; i++)
{
let up = Math.floor(n / i);
let low = Math.max(Math.floor(n / (i + 1)), root);
sum += (i * sumOfRange(up, low));
sum += (i * Math.floor(n / i));
}
return sum;
}
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
Share your thoughts in the comments
Please Login to comment...