![]() |
VOOZH | about |
Given a positive integer n. Find whether a number is a semiprime or not. Print True if number is semiprime else False. A semiprime is a natural number that is a product of two prime numbers.
Examples :
Input: 6
Output: True
Explanation
6 is a semiprime number as it is a
product of two prime numbers 2 and 3.
Input: 9
Output: True
Input: 8
Output: False
Approach: The approach is simple, factorize the given number by dividing it with the divisor of a number to remove the composite number. Meanwhile, keep updating the count variable of the prime number.
Below is the implementation of the above approach:
True False
Time Complexity: O()
Auxiliary space: O(1)
Another Approach: To check whether a number is a semiprime or not, the idea is to factorize the given number into its prime factors. If the number has exactly two prime factors, then it is a semiprime. Below are the steps:
Below is the implementation of the above approach:
True
Time Complexity: O(N*log(log(N)))
Auxiliary Space: O(1)