Sum of product of x and y such that floor ( n / x ) = y | Java 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 . ; n * ( n + 1 ) / 2. ; Return the sum of product x * y . ; Iterating i from 1 to sqrt ( n ) ; Finding the upper limit . ; Finding the lower limit . ; Driver Code
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 ) ) ; } }