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++
// CPP program to find // sum of given series #include <bits/stdc++.h> using namespace std; // function to find sum of series long 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 code int main() { long long int n = 35; cout << sum(n); return 0; } |
Java
// Java program to find // sum of given series import 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. |
Python3
# Python 3 program to find # sum of given series import math # function to find sum of series def 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 code n = 35print(sum(n)) # This code is contributed by Nikita Tiwari. |
C#
// C# program to find // sum of given series using 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 // PHP program to find // sum of given series // function to find // sum of series function 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) ?> |
Output:
131
Note: If observed closely, we can see that, if we take n common, series turns into an Harmonic Progression.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Sum of sum-series of first N Natural numbers
- Sum of series formed by difference between product and sum of N natural numbers
- Program to find sum of series 1 + 1/2 + 1/3 + 1/4 + .. + 1/n
- Efficient Program to Compute Sum of Series 1/1! + 1/2! + 1/3! + 1/4! + .. + 1/n!
- Find the sum of all the terms in the n-th row of the given series
- Program for sum of geometric series
- Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!
- Java Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!
- Program for sum of arithmetic series
- Series with largest GCD and sum equals to n
- Program to find Sum of a Series a^1/1! + a^2/2! + a^3/3! + a^4/4! +…….+ a^n/n!
- Sum of the series 1 + (1+3) + (1+3+5) + (1+3+5+7) + …… + (1+3+5+7+…+(2n-1))
- Sum of the Series 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + . . . . .
- Find the sum of the series 1+11+111+1111+..... upto n terms
- Sum of the Series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n
- Sum of the series 1, 3, 6, 10... (Triangular Numbers)
- Sum of the series 1 + (1+2) + (1+2+3) + (1+2+3+4) + ...... + (1+2+3+4+...+n)
- Program to get the Sum of series: 1 - x^2/2! + x^4/4! -.... upto nth term
- Program to find the sum of a Series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + ... + (n*n)
- Program to find sum of series 1*2*3 + 2*3*4+ 3*4*5 + . . . + n*(n+1)*(n+2)
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.
Improved By : manishshaw1

