Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

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.

AdvancedPhase 6 - Category 3: Loop & Conditional MixExample 9 of 10
semi-prime-numbers.py
1# Helper function
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
10# Find semi-primes
11print("Semi-prime numbers 1-100:")
12for num in range(1, 101):
13 found = False
14 for i in range(2, int(num ** 0.5) + 1):
15 if num % i == 0:
16 j = num // i
17 if is_prime(i) and is_prime(j):
18 found = True
19 break
20 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. 1

    Try divisors up to each candidate's square root.

  2. 2

    Find partner j with integer division.

  3. 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.