Summation of floor of harmonic progression | Java implementation of the approach ; Function to return the summation of the given harmonic series ; To store the summation ; Floor of sqrt ( n ) ; Summation of floor ( n / i ) ; From the formula ; Driver code
class GFG { static long getSum ( int n ) { long sum = 0 ; int k = ( int ) Math . sqrt ( n ) ; for ( int i = 1 ; i <= k ; i ++ ) { sum += Math . floor ( n / i ) ; } sum *= 2 ; sum -= Math . pow ( k , 2 ) ; return sum ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( getSum ( n ) ) ; } }