Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program for sum of the series: 1 + 1/2 + 1/3 + ... + 1/n
Mathematical series is one of the problems that helps us to improve problem-solving and logical thinking skills. One of the engaging problems is calculating the sum of the harmonic series. The series 1 + 1/2 + 1/3 + ? + 1/n is known as the harmonic series. In this article, we are going to learn how to find the sum of the series: 1 + 1/2 + 1/3 + ? + 1/n using Python.
What is the Harmonic Series?
The harmonic series is a mathematical sequence where each term is the reciprocal of a positive integer. It is represented as Hn = 1 + 1/2 + 1/3 + ? + 1/n, where Hn is the n-th harmonic number. This series grows logarithmically and is widely used in computational mathematics and algorithms.
Using Iterative Approach
We will first input the value of n, which determines how many terms to include in the series. Then, we use a loop to iterate from 1 to n, adding the reciprocal of each number to a cumulative sum. This approach is straightforward and helps in understanding the series computation.
Steps for Implementation
- Initialize a variable to store the sum of the series.
- Use a loop to iterate through numbers from 1 to n.
- Add the reciprocal of the current number to the cumulative sum.
- Print the result after the loop completes.
Example
n = 5
series_sum = 0.0
for i in range(1, n + 1):
series_sum += 1 / i
print(f"The sum of the series is: {series_sum}")
The sum of the series is: 2.283333333333333
Time Complexity: O(n)
Space Complexity: O(1)
Using Recursive Approach
In this approach, we calculate the sum of the series using a recursive function. The function adds the reciprocal of the current number to the result of the series for the remaining terms.
Steps for Implementation
- Define a recursive function that takes n as a parameter.
- If n equals 1, return 1 (base case).
- Otherwise, return 1/n + recursive function call with n?1.
- Call the function and print the result.
Example
def harmonic_sum(n):
if n == 1:
return 1
return 1 / n + harmonic_sum(n - 1)
n = 5
series_sum = harmonic_sum(n)
print(f"The sum of the series is: {series_sum}")
The sum of the series is: 2.283333333333333
Time Complexity: O(n)
Space Complexity: O(n) due to recursion stack
Using Mathematical Formula
For very large values of n, we can approximate the sum of the harmonic series using the formula: Hn ? ln(n) + ?, where ln(n) is the natural logarithm of n, and ? is the Euler-Mascheroni constant (? 0.5772).
Steps for Implementation
- Calculate the natural logarithm of n using the math module.
- Add the Euler-Mascheroni constant to approximate the sum.
- Print the result.
Example
import math
n = 100
gamma = 0.5772156649
series_sum = math.log(n) + gamma
print(f"The approximate sum of the series is: {series_sum}")
The approximate sum of the series is: 5.187377517639621
Time Complexity: O(1)
Space Complexity: O(1)
Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Iterative | O(n) | O(1) | Small to medium n |
| Recursive | O(n) | O(n) | Understanding recursion |
| Mathematical Formula | O(1) | O(1) | Large n (approximation) |
Conclusion
The iterative approach is most efficient for exact calculations with reasonable values of n. For very large n, use the mathematical formula for quick approximation. The recursive approach is useful for learning but has higher space complexity.
