0
0
PythonProgramBeginner · 2 min read

Python Program to Find Sum of Series 1/1+1/2+1/3+...+1/n

You can find the sum of the series 1/1 + 1/2 + 1/3 + ... + 1/n in Python by using a loop like sum_series = 0; for i in range(1, n+1): sum_series += 1/i.
📋

Examples

Input1
OutputSum of series up to 1 terms is 1.0
Input5
OutputSum of series up to 5 terms is 2.283333333333333
Input10
OutputSum of series up to 10 terms is 2.9289682539682538
🧠

How to Think About It

To find the sum of the series 1/1 + 1/2 + 1/3 + ... + 1/n, think of adding fractions where the numerator is always 1 and the denominator goes from 1 up to n. You start with zero and keep adding each fraction one by one until you reach the nth term.
📐

Algorithm

1
Get the input number n from the user.
2
Initialize a variable sum_series to 0 to store the total.
3
Use a loop to go from 1 to n.
4
In each loop step, add 1 divided by the current number to sum_series.
5
After the loop ends, return or print the sum_series.
💻

Code

python
n = int(input("Enter the number of terms: "))
sum_series = 0.0
for i in range(1, n + 1):
    sum_series += 1 / i
print(f"Sum of series up to {n} terms is {sum_series}")
Output
Enter the number of terms: 5 Sum of series up to 5 terms is 2.283333333333333
🔍

Dry Run

Let's trace the program for n=5 through the code

1

Input

User enters n = 5

2

Initialize sum

sum_series = 0.0

3

Loop iteration 1

i=1, sum_series = 0.0 + 1/1 = 1.0

4

Loop iteration 2

i=2, sum_series = 1.0 + 1/2 = 1.5

5

Loop iteration 3

i=3, sum_series = 1.5 + 1/3 ≈ 1.8333

6

Loop iteration 4

i=4, sum_series = 1.8333 + 1/4 = 2.0833

7

Loop iteration 5

i=5, sum_series = 2.0833 + 1/5 = 2.2833

8

Print result

Output: Sum of series up to 5 terms is 2.283333333333333

isum_series
11.0
21.5
31.8333333333333333
42.083333333333333
52.283333333333333
💡

Why This Works

Step 1: Initialize sum

We start with sum_series = 0.0 because we have not added any terms yet.

Step 2: Add each term

For each number from 1 to n, we add 1/i to the sum to include that term in the series.

Step 3: Final sum

After the loop finishes, sum_series holds the total sum of all terms from 1 to n.

🔄

Alternative Approaches

Using while loop
python
n = int(input("Enter the number of terms: "))
sum_series = 0.0
i = 1
while i <= n:
    sum_series += 1 / i
    i += 1
print(f"Sum of series up to {n} terms is {sum_series}")
This uses a while loop instead of for loop; both are equally readable but for loop is more concise.
Using sum and generator expression
python
n = int(input("Enter the number of terms: "))
sum_series = sum(1 / i for i in range(1, n + 1))
print(f"Sum of series up to {n} terms is {sum_series}")
This is a compact and Pythonic way using built-in sum and generator expression.

Complexity: O(n) time, O(1) space

Time Complexity

The program runs a single loop from 1 to n, so it performs n additions, making it O(n).

Space Complexity

Only a few variables are used to store the sum and loop counter, so space is O(1).

Which Approach is Fastest?

All approaches use a single loop or generator expression, so they have similar speed; the generator expression is concise but may be slightly slower due to function call overhead.

ApproachTimeSpaceBest For
For loopO(n)O(1)Clear and simple for beginners
While loopO(n)O(1)Alternative loop style
Generator expression with sumO(n)O(1)Concise and Pythonic
💡
Use a for loop with range starting at 1 to avoid division by zero.
⚠️
Starting the loop from 0 causes division by zero error since 1/0 is undefined.