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 functions
2def is_prime(n):
3 if n < 2:
4 return False
5 for i in range(2, int(n ** 0.5) + 1):
6 if n % i == 0:
7 return False
8 return True
9
10def count_factors(n):
11 count = 0
12 for i in range(1, n + 1):
13 if n % i == 0:
14 count += 1
15 return count
16
17# Find numbers
18print("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