Sum of Primes in Range
Calculate sum of all primes in range.
IntermediatePhase 6 - Category 3: Loop & Conditional MixExample 2 of 10
sum-of-primes-in-range.py
1# Helper function2def is_prime(n):3 if n < 2:4 return False5 for i in range(2, int(n ** 0.5) + 1):6 if n % i == 0:7 return False8 return True910# Take range11a = int(input("Enter start: "))12b = int(input("Enter end: "))1314# Sum primes15total = 016for num in range(a, b + 1):17 if is_prime(num):18 total += num1920print(f"Sum of primes: {total}")
Output
Enter start: 1 Enter end: 10 Sum of primes: 17
What's going on
Find primes and accumulate sum.
Key Concepts:
Check each number for prime
Add to sum if prime
Return total sum
🔗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