Company: Yahoo
Question:Given a no N, the task is to find the largest prime factor of the number.
Input:
The first line of input contains an integer T, denoting the no of test cases. Then T test cases follow. Each test case contains an integer N.
Output:
For each test case in a new line print the largest prime factor of N.
Constraints:
1<=T<=100
2<=N<=10^11+1
Example:
Input:
2
6
15
Output:
3
5
CODE:
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void main (String[] args) {
Scanner sc = new Scanner (System.in);
int m = sc.nextInt();
for(int mi=0;mi<m;mi++){
int n = sc.nextInt();
lpf(n);
}
}
public static void lpf(int n ){
int num=0;
while(n%2==0){
n/=2;
}
if(n%2==0){num=2;}
for(int i=3;i<Math.sqrt(n);i+=2){
if(n%i==0 && i>num){num=i;}
while(n%i==0){
n/=i;
}
}
if(n>2 && n>num){num=n;}
System.out.println(num);
}
}
EXECUTION TIME: 0.1
Question:Given a no N, the task is to find the largest prime factor of the number.
Input:
The first line of input contains an integer T, denoting the no of test cases. Then T test cases follow. Each test case contains an integer N.
Output:
For each test case in a new line print the largest prime factor of N.
Constraints:
1<=T<=100
2<=N<=10^11+1
Example:
Input:
2
6
15
Output:
3
5
CODE:
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void main (String[] args) {
Scanner sc = new Scanner (System.in);
int m = sc.nextInt();
for(int mi=0;mi<m;mi++){
int n = sc.nextInt();
lpf(n);
}
}
public static void lpf(int n ){
int num=0;
while(n%2==0){
n/=2;
}
if(n%2==0){num=2;}
for(int i=3;i<Math.sqrt(n);i+=2){
if(n%i==0 && i>num){num=i;}
while(n%i==0){
n/=i;
}
}
if(n>2 && n>num){num=n;}
System.out.println(num);
}
}
EXECUTION TIME: 0.1
Comments
Post a Comment