Improving the algorithm
101 / 2 = 50 remainder 1 101 / 3 = 33 remainder 2 101 / 4 = 25 remainder 1 101 / 5 = 20 remainder 1 101 / 6 = 16 remainder 5 101 / 7 = 14 remainder 3 101 / 8 = 12 remainder 5 101 / 9 = 11 remainder 2 101 / 10 = 10 remainder 1 101 / 11 = 9 remainder 2 101 / 12 = 8 remainder 5 101 / 13 = 7 remainder 10 ...
Big performance gain:
public static boolean isPrime(int candidate) {
for (int i = 2; i * i < candidate; i++) {
if (0 == candidate % i) {
return false;
}
}
return candidate != 1;
}
public static boolean isPrime(int candidate) {
//for (int i = 2; i * i < candidate; i++) {
for (int i = 2; i * i <= candidate; i++) {
if (0 == candidate % i) {
return false;
}
}
return candidate != 1;
}