Semi-Prime Numbers

Find semi-prime numbers (product of two primes).

Logic BuildingAdvanced
Logic Building
# Helper function
def is_prime(n):
    if n < 2:
        return False
    for i in range(2, int(n ** 0.5) + 1):
        if n % i == 0:
            return False
    return True

# Find semi-primes
print("Semi-prime numbers 1-100:")
for num in range(1, 101):
    found = False
    for i in range(2, int(num ** 0.5) + 1):
        if num % i == 0:
            j = num // i
            if is_prime(i) and is_prime(j):
                found = True
                break
    if found:
        print(num, end=" ")
print()

Output

Semi-prime numbers 1-100:
4 6 9 10 14 15 21 22 25 26 ...

Check if number is product of two primes.

Key Concepts:

  • Find factors
  • Check if both factors are prime
  • Semi-prime property