Advertisement
Guest User

Untitled

a guest
Sep 30th, 2016
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. public class LargestPrimeFactor {
  2.  
  3. public static void main(String[] args) {
  4. Scanner scan = new Scanner(System.in);
  5. int cases = scan.nextInt();
  6. int num = 0, temp = 0;
  7. while(cases!=0) {
  8. num = scan.nextInt();
  9. for(int i=2; i<=num; i++)
  10. if(num%i==0) {
  11. if(isPrime(i)) {
  12. if(temp<i)
  13. temp = i; //to always have the largest factor
  14. }
  15. num/=i; // to reduce the iterations for a large number.
  16. }
  17. System.out.println(temp);
  18. cases--;
  19. }
  20. }
  21.  
  22. public static boolean isPrime(int num) {
  23. if ( num > 2 && num%2 == 0 )
  24. return false;
  25. int top = (int)Math.sqrt(num) + 1;
  26. for(int i = 3; i < top; i+=2)
  27. if(num % i == 0)
  28. return false;
  29. return true;
  30. }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement