Harmonic Series Sum

Calculate 1 + 1/2 + 1/3 + ... + 1/n.

Logic BuildingIntermediate
Logic Building
# Take n
n = int(input("Enter n: "))

# Calculate harmonic sum
total = 0.0
for i in range(1, n + 1):
    total += 1.0 / i

print(f"Harmonic sum: {total}")

Output

Enter n: 5
Harmonic sum: 2.283333333333333

Sum reciprocals of numbers.

Key Concepts:

  • Loop from 1 to n
  • Add 1/i to sum
  • Use float division