Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LargestPrimeFactor {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- int cases = scan.nextInt();
- int num = 0, temp = 0;
- while(cases!=0) {
- num = scan.nextInt();
- for(int i=2; i<=num; i++)
- if(num%i==0) {
- if(isPrime(i)) {
- if(temp<i)
- temp = i; //to always have the largest factor
- }
- num/=i; // to reduce the iterations for a large number.
- }
- System.out.println(temp);
- cases--;
- }
- }
- public static boolean isPrime(int num) {
- if ( num > 2 && num%2 == 0 )
- return false;
- int top = (int)Math.sqrt(num) + 1;
- for(int i = 3; i < top; i+=2)
- if(num % i == 0)
- return false;
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement