Sum of series (n/1) + (n/2) + (n/3) + (n/4) +.......+ (n/n)

20 Apr, 2021
Given a value n, find the sum of series, (n/1) + (n/2) + (n/3) + (n/4) +.......+(n/n) where the value of n can be up to 10^12. Note: Consider only integer division.Examples:  
Input : n = 5
Output : (5/1) + (5/2) + (5/3) + 
        (5/4) + (5/5) = 5 + 2 + 1 + 1 + 1 
                      = 10

Input : 7
Output : (7/1) + (7/2) + (7/3) + (7/4) +
         (7/5) + (7/6) + (7/7) 
         = 7 + 3 + 2 + 1 + 1 + 1 + 1 
         = 16
 
Below is the program to find the sum of given series:  
C++
Java
Python3
C#
PHP
Javascript
// CPP program to find// sum of given series#include <bits/stdc++.h>using namespace std; // function to find sum of serieslong long int sum(long long int n){    long long int root = sqrt(n);    long long int ans = 0;     for (int i = 1; i <= root; i++)        ans += n / i;         ans = 2 * ans - (root * root);    return ans;} // driver codeint main(){    long long int n = 35;    cout << sum(n);    return 0;}
// Java program to find// sum of given seriesimport java.util.*; class GFG {         // function to find sum of series    static long sum(long n)    {        long root = (long)Math.sqrt(n);        long ans = 0;              for (int i = 1; i <= root; i++)            ans += n / i;                  ans = 2 * ans - (root * root);                 return ans;    }         /* Driver code */    public static void main(String[] args)    {        long n = 35;        System.out.println(sum(n));    }}     // This code is contributed by Arnav Kr. Mandal.       
# Python 3 program to find# sum of given series import math # function to find sum of seriesdef sum(n) :    root = (int)(math.sqrt(n))    ans = 0      for i in range(1, root + 1) :        ans = ans + n // i          ans = 2 * ans - (root * root)    return ans # driver coden = 35print(sum(n)) # This code is contributed by Nikita Tiwari.
// C# program to find// sum of given seriesusing System; class GFG {         // Function to find sum of series    static long sum(long n)    {        long root = (long)Math.Sqrt(n);        long ans = 0;             for (int i = 1; i <= root; i++)            ans += n / i;                 ans = 2 * ans - (root * root);                 return ans;    }         // Driver code    public static void Main()    {        long n = 35;        Console.Write(sum(n));    }}     // This code is contributed vt_m.
<?php// PHP program to find// sum of given series // function to find// sum of seriesfunction sum($n){    $root = intval(sqrt($n));    $ans = 0;     for ($i = 1; $i <= $root; $i++)        $ans += intval($n / $i);     $ans = (2 * $ans) -           ($root * $root);    return $ans;} // Driver code$n = 35;echo (sum($n)); // This code is contributed by// Manish Shaw(manishshaw1)?>
<script>// Javascript program to find// sum of given series // function to find// sum of seriesfunction sum(n){    let root = parseInt(Math.sqrt(n));    let ans = 0;     for (let i = 1; i <= root; i++)        ans += parseInt(n / i);     ans = (2 * ans) -        (root * root);    return ans;} // Driver codelet n = 35;document.write(sum(n)); // This code is contributed by gfgking.</script>
Output:  
131
Note: If observed closely, we can see that, if we take n common, series turns into an Harmonic Progression. 
manishshaw1
gfgking
harmonic progression
series
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Algorithm to solve Rubik's Cube
Merge two sorted arrays with O(1) extra space
Program to print prime numbers from 1 to N.
Find next greater number with same set of digits
Segment Tree | Set 1 (Sum of given range)
Check if a number is Palindrome
Count ways to reach the n'th stair
Fizz Buzz Implementation
Count all possible paths from top left to bottom right of a mXn matrix
Product of Array except itself