Numbers with Prime Number of Factors
Find numbers whose factor count is prime.
AdvancedPhase 6 - Category 3: Loop & Conditional MixExample 8 of 10
numbers-with-prime-number-of-factors.py
1# Helper functions2def 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 True910def count_factors(n):11 count = 012 for i in range(1, n + 1):13 if n % i == 0:14 count += 115 return count1617# Find numbers18print("Numbers 1-100 with prime number of factors:")19for num in range(1, 101):20 factor_count = count_factors(num)21 if is_prime(factor_count):22 print(num, end=" ")23print()
Output
Numbers 1-100 with prime number of factors: 2 3 4 5 7 9 11 13 17 19 ...
What's going on
Count factors and check if count is prime.
Key Concepts:
Count factors of number
Check if count is prime
Filter numbers
🔗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