Problem of the day
You are given a positive integer 'N'. Your task is to return all the prime numbers less than or equal to the 'N'.
Note:
1) A prime number is a number that has only two factors: 1 and the number itself.
2) 1 is not a prime number.
The first line contains an integer 'T', which denotes the number of test cases or queries to be run. Then, the 'T' test cases follow.
The first line of each test case contains only one integer 'N', as described in the problem statement.
Output Format:
For each test case, return a sequence of integers denoting the prime number less than or equal to 'N' in the increasing order.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 100
2 <= N <= 5000
Time Limit: 1sec
1
5
2 3 5
2,3 and 5 are the only prime numbers less than or equal to 5.
1
8
2 3 5 7
2,3,5 and 7 are the only prime numbers less than or equal to 8.
Think of the Brute Force approach.
The idea here is to check every number less than or equal to N is prime or not. Those who are prime just add them in an array and return the array at the end of the function.
bool 'IS_PRIME'(int n):
O(N^2), where ‘N’ is the number given in the problem.
We are checking for each number in the range 2 to ‘N', and for each number, we are further running a loop till ‘N-1’ to check whether it is prime or not. So, we are using a nested loop which gives a time complexity of O(N^2).
O(N), where ‘N’ is the number given in the problem.
Although the size of the result array is equal to the number of prime numbers less than or equal to ‘N’, this value increase as the ‘N’ value increases. So, we can mention it as O(N).