publicstaticvoidmain(String[] args){ // Scanner in = new Scanner(System.in); // int n = in.nextInt(); // in.close(); Stack<Integer> a = new Stack<>(), b = new Stack<>(); int n = 100000; long s = System.currentTimeMillis(), e; for (int i = 1; i < n; i++) { if (f(i)) a.push(i); } e = System.currentTimeMillis(); System.out.println(e - s + " ms"); s = System.currentTimeMillis(); for (int i = 1; i < n; i++) { if (func(i)) b.push(i); } e = System.currentTimeMillis(); System.out.println(e - s + " ms"); System.gc(); }
staticbooleanf(int n){ if (n <= 3) return n > 1; for (int i = 2; i < n; i++) { if (n % i == 0) { returnfalse; } } returntrue; }
staticbooleanfunc(int n){ if (n <= 3) return n > 1; if (n % 6 != 1 && n % 6 != 5) { returnfalse; } int tem = (int) Math.sqrt(n); for (int i = 5; i <= tem; i += 6) { if (n % i == 0 || n % (i + 2) == 0) returnfalse; } returntrue; } }