Alternating Series Sum
Calculate 1 - 1/2 + 1/3 - 1/4 + ... ± 1/n.
IntermediatePhase 2 - Level 3: Mathematical & Logical PatternsExample 13 of 13
alternating-series-sum.py
1# Take n2n = int(input("Enter n: "))34# Calculate sum5sum_alt = 06for i in range(1, n + 1):7 if i % 2 == 1:8 sum_alt += 1 / i9 else:10 sum_alt -= 1 / i1112print(f"Sum: {sum_alt:.4f}")
Output
Enter n: 5 Sum: 0.7833
What's going on
Alternate signs based on position.
Key Concepts:
Add for odd positions
Subtract for even positions
Create alternating pattern
🔗Related Content
- 📖
Phase 2 - Learn Concepts
Review Phase 2 concepts and explanations
- 📝
Phase 2 - Quiz
Test your Phase 2 understanding with quiz questions
- 💻
Phase 2 - All Practice Problems
Explore all practice problems for Phase 2
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum