
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
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}")Output
The sum of the series is: 2.283333333333333
The time Complexity for this approach is : O(n)
The space complexity for this approach is : O(n)
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/? + recursive function call with n?1.
- Call the function and print the result.
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}")Output
The sum of the series is: 2.283333333333333
The Time Complexity for this approach is : O(n)
The Space Complexity for this approach is :O(n)
Using Mathematical Formula
If we are given a very large value of n, we can approximate the sum of the harmonic series using the formula, ?? ? ln(?) + ?. Where, ln(n) is the natural logarithm of n, and ? is the Euler-Mascheroni constant ( ? 0.577).
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.
import math n = 100 gamma = 0.5772156649 series_sum = math.log(n) + gamma print(f"The approximate sum of the series is: {series_sum}")Output
The approximate sum of the series is: 5.187377517639621
The Time Complexity of this approach is : O(1)
The Space Complexity of this approach is : O(1)