We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
public static long isPrime(long n) {
long maxPrime = -1;
while (n % 2 == 0) {
maxPrime = 2;
n /= 2;
}
while (n % 3 == 0) {
maxPrime = 3;
n /= 3;
}
for (int i = 5; i <= Math.sqrt(n); i += 6) {
while (n % i == 0) {
maxPrime = i;
n = n / i;
}
while (n % (i + 2) == 0) {
maxPrime = i + 2;
n = n / (i + 2);
}
}
if (n > 4)
maxPrime = n;
return maxPrime;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int a0 = 0; a0 < t; a0++){
long n=in.nextLong();
System.out.println(isPrime(n));
}
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #3: Largest prime factor
You are viewing a single comment's thread. Return to all comments →
import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.*;
public class Solution {
}