Semi-Prime Numbers
Find semiprimes from one through one hundred using Python. Students search for two prime factors and allow the same prime twice in a valid product.
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# Find semi-primes11print("Semi-prime numbers 1-100:")12for num in range(1, 101):13 found = False14 for i in range(2, int(num ** 0.5) + 1):15 if num % i == 0:16 j = num // i17 if is_prime(i) and is_prime(j):18 found = True19 break20 if found:21 print(num, end=" ")22print()
Output
...
The sample abbreviates a longer result. The program prints the intervening values; it does not print these dots.
In this one
- Build prime factor pairs
- Allow equal factors
- Stop after a witness
How it works
- 1
Try divisors up to each candidate's square root.
- 2
Find partner j with integer division.
- 3
Set found when both factors are prime, then print once.
Edge cases
Are prime squares semiprime?
Are prime squares semiprime?
Yes. The two prime factors may be equal.
Does a prime number itself qualify?
Does a prime number itself qualify?
No. It is not a product of two primes.
What's going on
For each candidate, the inner loop tries divisors through its square root. A found divisor i supplies the partner j = num // i.
Both factors must be prime. Thus four qualifies as two times two, while twelve does not because every factor pair includes a composite factor. A found flag prints each candidate once and break ends only its factor search.
Are prime squares semiprime? Yes. The two prime factors may be equal.
Does a prime number itself qualify? No. It is not a product of two primes.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Which value qualifies?
Pick an answer to check it.
Question 2
Are prime squares semiprime?
Pick an answer to check it.
Questions people ask
Are prime squares semiprime?
Yes. The two prime factors may be equal.
Does a prime number itself qualify?
No. It is not a product of two primes.
Which value qualifies?
6.
๐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