Harmonic Series Sum
Calculate 1 + 1/2 + 1/3 + ... + 1/n.
IntermediatePhase 6 - Category 4: Mathematical CombinationsExample 4 of 10
harmonic-series-sum.py
1# Take n2n = int(input("Enter n: "))34# Calculate harmonic sum5total = 0.06for i in range(1, n + 1):7 total += 1.0 / i89print(f"Harmonic sum: {total}")
Output
Enter n: 5 Harmonic sum: 2.283333333333333
What's going on
Sum reciprocals of numbers.
Key Concepts:
Loop from 1 to n
Add 1/i to sum
Use float division
🔗Related Content
- 📖
Phase 6 - Learn Concepts
Review Phase 6 concepts and explanations
- 📝
Phase 6 - Quiz
Test your Phase 6 understanding with quiz questions
- 💻
Phase 6 - All Practice Problems
Explore all practice problems for Phase 6
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum