Check if a Number is Semi-Prime
In various cryptographic algorithms and number theory applications, numbers with specific prime factor properties play a crucial role. One such category is semi-prime numbers, which are integers that are the product of exactly two prime numbers (they can be the same). Understanding and identifying these numbers is fundamental for tasks like key generation and factorization challenges.
Implement a function that determines if a given positive integer n is a semi-prime number.
n: An integer representing the number to check.true if n is a semi-prime number, false otherwise.n will always be a positive integer.Examples
Example 1
Input:
n = 6
Output:
true
Note:
6 is 2 * 3. Both 2 and 3 are prime numbers.
Example 2
Input:
n = 4
Output:
true
Note:
4 is 2 * 2. 2 is a prime number, and 4 is a product of two primes (2 and 2).
Example 3
Input:
n = 12
Output:
false
Note:
12 is 2 * 2 * 3. It has three prime factors, not two.
Example 4
Input:
n = 7
Output:
false
Note:
7 is a prime number itself. It has only one prime factor.
Example 5
Input:
n = 1
Output:
false
Note:
1 is not considered a semi-prime as it is not a product of two primes.
Constraints
1 <= n <= 10^6Loading editor...