Count Primes in Range
Count prime numbers between a and b.
AdvancedPhase 2 - Level 5: Logical Loop CombinationsExample 12 of 12
count-primes-in-range.py
1# Take range2a = int(input("Enter start: "))3b = int(input("Enter end: "))45# Count primes6count = 07for num in range(a, b + 1):8 if num < 2:9 continue10 is_prime = True11 for i in range(2, int(num ** 0.5) + 1):12 if num % i == 0:13 is_prime = False14 break15 if is_prime:16 count += 11718print(f"Primes in range: {count}")
Output
Enter start: 10 Enter end: 30 Primes in range: 6
What's going on
Check each number in range for primality.
Key Concepts:
Loop through range
Check each for prime
Count matches
🔗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