Summation of floor of harmonic progression

Given an integer N, the task is to find the summation of the harmonic series \sum_{i=1}^{n} \lfloor{n/i}\rfloor.

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 complexiy of this approach will be O(n).

Efficient approach: Use the following formula to calculate the summation of the series:
 $\sum_{i=1}^n\lfloor\frac ni\rfloor=2\sum_{i=1}^k\lfloor\frac ni\rfloor-k^2$ for $k=\lfloor\sqrt n\rfloor$
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:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the summation of
// the given harmonic series
long long int getSum(int n)
{
  
    // To store the summation
    long long int sum = 0;
  
    // Floor of sqrt(n)
    int k = sqrt(n);
  
    // Summation of floor(n / i)
    for (int i = 1; i <= k; i++) {
        sum += floor(n / i);
    }
  
    // From the formula
    sum *= 2;
    sum -= pow(k, 2);
  
    return sum;
}
  
// Driver code
int main()
{
    int n = 5;
  
    cout << getSum(n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
from math import floor, sqrt, ceil
  
# Function to return the summmation of
# the given harmonic series
def getSum(n):
  
    # To store the summmation
    summ = 0
  
    # Floor of sqrt(n)
    k =(n)**(.5)
  
    # Summation of floor(n / i)
    for i in range(1, floor(k) + 1):
        summ += floor(n / i)
  
    # From the formula
    summ *= 2
    summ -= pow(floor(k), 2)
  
    return summ
  
# Driver code
n = 5
  
print(getSum(n))
  
# This code is contributed by Mohit Kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
using System;
      
class GFG 
{
      
    // Function to return the summation of 
    // the given harmonic series 
    static double getSum(int n) 
    
      
        // To store the summation 
        double sum = 0;
      
        // Floor of sqrt(n) 
        int k = (int)Math.Sqrt(n); 
      
        // Summation of floor(n / i) 
        for (int i = 1; i <= k; i++)
        
            sum += Math.Floor((double)n / i); 
        
      
        // From the formula 
        sum *= 2; 
        sum -= Math.Pow(k, 2); 
      
        return sum; 
    
      
    // Driver code 
    public static void Main (String[] args)
    
        int n = 5; 
      
        Console.WriteLine(getSum(n)); 
    
}
      
// This code is contributed by PrinciRaj1992

chevron_right


Output:

10


My Personal Notes arrow_drop_up

Second year Department of Information Technology Jadavpur University

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.