Summation of floor of harmonic progression | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int getSum ( int n ) { long long int sum = 0 ; int k = sqrt ( n ) ; for ( int i = 1 ; i <= k ; i ++ ) { sum += floor ( n / i ) ; } sum *= 2 ; sum -= pow ( k , 2 ) ; return sum ; } int main ( ) { int n = 5 ; cout << getSum ( n ) ; return 0 ; }