Largest Prime Factor
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.
Input Format :
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.
Constraints :
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
CodingNinjas
author
2y
Brute Force
- If the given integer is 1, then return -1 as there is no prime factor of 1.
- Initialize a variable ‘largestFactor’:= 0, it will store the largest prime factor of the given number.
- Run a loop w...read more
CodingNinjas
author
2y
Upside-Down Division
In the upside-down division or repeated division method of finding prime factorization, we repeatedly divide the number with its smallest factor to find its prime factorization. We...read more
Help your peers!
Add answer anonymously...
Top TCS Software Developer Intern interview questions & answers
Popular interview questions of Software Developer Intern
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app