You are given a positive integer ‘n’. Your task is to find the largest prime factor of this given positive integer.
Note :If there is no prime factor of a given integer, then print -1.
The first line of input contains an integer ‘T’ denoting the number of test cases.
The first and only line of each test case contains a positive integer ‘n’.
Output Format :
For each test case, print the largest prime factor of the given positive integer in a separate line.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 50
1 <= n <= 10^9
Where ‘T’ is the number of test cases and ‘n’ is the size of the array.
Time Limit: 1 sec
2
5
27
5
3
Test case 1:
5 is a prime number, so its largest prime is 5 itself
Test case 2:
Prime factorization of 27 is 3*3*3.
Thus, it's an only prime factor is 3.
2
14
30
7
5
Test case 1 :
Prime factorization of 14 is 2*7.
It has two prime factors, 2 and 7, the largest prime number out of them is 7.
Test case 2 :
Prime factorization of 30 is 2*3*5.
It has three prime factors, 2, 3, and 5, the largest of them is 5.
One by one check for all positive integers between 2 and ‘n’ (inclusive).
O(n*sqrt(n)), where ‘n’ is the given positive integer.
In the worst case, it takes sqrt(n) time to check whether the given number is prime or not. Here, in the worst case, we have to check for ‘n’ integers whether they are prime or not, which makes its complexity O(n*sqrt(n)).
O(1)
We are not using any extra space here.