VOOZH about

URL: https://www.geeksforgeeks.org/dsa/summation-of-floor-of-harmonic-progression/

⇱ Summation of floor of harmonic progression - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Summation of floor of harmonic progression

Last Updated : 13 Sep, 2022

Given an integer N, the task is to find the summation of the harmonic series .

Examples: 

Input: N = 5 
Output: 10 
floor(3/1) + floor(3/2) + floor(3/3) = 3 + 1 + 1 = 5
Input: N = 20 
Output: 66 
 


 


Naive approach: Run a loop from 1 to N and find the summation of the floor values of N / i. Time complexity of this approach will be O(n).
Efficient approach: Use the following formula to calculate the summation of the series: 

Now, the loop needs to be run from 1 to sqrt(N) and the time complexity gets reduced to O(sqrt(N))
Below is the implementation of the above approach: 
 


Output: 
10

 

Time Complexity: O(sqrt(n)), since the for loop runs for sqrt(n) times.
Auxiliary Space: O(1), since no extra space has been taken.

Comment