Let us call a number prime? if it has exactly two distinct prime divisors. For example, numbers 6, 18, 24 are prime?, while 4, 8, 9, 42 are not. Find the number of prime? numbers which are between 1 and ‘N’, inclusive.
Example :‘N’ = 7
6 is the only prime? number in 1-7. So, the answer is 1.
The first line contains a single integer ‘T’ denoting the number of test cases, then the test case follows.
The first line of each test case contains a single integer, ‘N’.
Output Format :
For each test case, return a single integer, the number of prime? numbers between 1 and ‘N’ (inclusive).
Output for each test case will be printed on a separate line.
Note :
You are not required to print anything; it has already been taken care of. Just implement the function.
1 ≤ T ≤ 10
1 ≤ N ≤ 500
Time limit: 1 sec
2
10
5
2
0
For the first test case, 6 and 10 are the prime? Numbers. 6 = 3x2 and 10 = 5x2
For the second test case, there are no prime? Numbers till 5.
2
12
20
3
7
Since the constraints on ‘N’ are quite small, can we do something like a brute-force?
Approach:
Algorithm:
O(N.sqrt(N)), where ‘N’ is the range given to us.
Since we can factorize a number N in sqrt(N) time, we are factorizing every number from 1 to N. Hence the time complexity is O(N.sqrt(N)).
Hence, the time complexity is O(N.sqrt(N)).
O(N), where ‘N’ is the range given to us.
We use an array to mark which numbers are prime and which are not.
Hence, the space complexity is O(N).