A Sphenic Number is defined as a positive integer N which can be written as a product of exactly three distinct primes. The first few sphenic numbers are 30, 42, 66, 70, 78, 102, 105, 110, 114, …
Your task is that for a given number N, you have to determine whether it is a Sphenic Number or not.
Example:
Let N = 42
N = 2 * 3 * 7
Here, 2, 3 and 7 are 3 distinct prime numbers. Thus, 42 is a sphenic number.
Let N = 60
N = 2 * 2 * 3 * 5
Here, 2, 3, and 5 are distinct prime numbers, but 2 is repeated twice. A sphenic number is a product of exactly 3 distinct primes. Thus, 60 is not a sphenic number.
Let N = 15
N = 3 * 5
Here, 3 & 5 are only 2 distinct prime numbers but, we need 3 distinct prime numbers. Thus, 15 is not a sphenic number.
The first line of input contains an integer 'T' denoting the number of queries or test cases.
The first and only line of each input contains an integer 'N'.
Output format:
For each test case, print ‘Yes’ if the given number is a sphenic number; otherwise, print ‘No’ in a separate line.
The output of each test case should be printed in a separate line.
Note:
You do not need to print anything, and it has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= N <= 10^8
where 'T' is the number of test cases and 'N' is the given integer.
Time Limit: 1 sec.
2
121
138
No
Yes
For test case 1:
N = 121
N = 11 * 11
As it is not a multiple of 3 distinct prime numbers. Thus, 121 is not a sphenic number.
For test case 2:
N = 138
N = 2 * 3 * 23
As 2, 3 & 23 are 3 distinct prime numbers. Thus, 138 is a sphenic number.
3
78
57
125
Yes
No
No
The very first approach can be using the sieve of Eratosthenes for doing prime factorization of a number.
Note: We are using Sieve here but as the constraints are too big, we won't be able to create the sieve array in range of N. Thus, this is only a theoretical explanation.
Let’s take an example where N = 30
→ First iteration:
As the smallest prime factor of 30 is 2 i.e. sieve[30] = 2, so, we divide 30 by 2 and increase the count to 1 and update prev to 2.
We update 30 with 30 / 2 i.e. 15.
→ Second iteration:
Now, the smallest prime factor of 15 is 3 i.e. sieve[15] = 3, so, we divide 15 by 3 and increase the count to 2 and update prev to 3.
We update 15 with 15 / 3 i.e. 5.
→ Third iteration:
The smallest prime factor of 5 is 5 i.e. sieve[5] = 5, so, we divide 5 by 5 and increase the count to 3 and update prev to 5.
We update 5 with 5 / 5 i.e. 1.
As, N becomes equal to 1, so, while loop will be ended and as the count is equal to 3, it is a sphenic number.
The time complexity analysis can be done as follows:
Pre Computation time:
O(Nlog(log(N)) where N is the largest possible number(constraint). For more information, you may refer here.
Approach time:
O(1), per test case.
In the worst case, the loop will run for at max 3 times which is constant.
Note: This is a very fast algorithm for small values of N.
The space complexity analysis can be done as follows:
Pre Computation time:
O(N) where N is the largest possible number(constraint).
In the worst case, we need to create an array of size N.
Approach time:
O(1), per test case.
In the worst case, only constant extra space is used.