如何优化此代码,以便时间永远不会超过2000毫秒 变量输入(< 10 ^ 5&&>>> 1)是要检查为tprime的数字的计数 另一个下一行扫描所有数字,范围从(> 1&&< 10 ^ 12)。
import java.util.Scanner;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class TprimePractise {
public static void main(String[] args) throws Exception {
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
int input = Integer.valueOf(line);
line = br.readLine();
long sqrt, x;
for (String num : line.split("\\s")) {
x = Long.valueOf(num);
if (x == 1) {
System.out.println("NO");
continue;
}
sqrt = (long) Math.sqrt(x);
if (sqrt * sqrt == x && isPrime(sqrt)) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
public static boolean isPrime(long num) {
for (int j = (int) num / 2; j >= 2; j--) {
if (num % j == 0) {
return false;
}
}
return true;
}
}