Hi all,
I made the following method to find the (n)th prime number, but I was wondering what would make it more efficient/if there is a better algorithm to do so?
int mostRecentPrime = 3;
for (int i = 2; i < primes; i++) {
for (int add = 2; true; add+=2) {
boolean hasBeenBroken = false;
int peak = (int)Math.sqrt(mostRecentPrime + add)+1;
for (int ind = 3; ind <= peak; ind+=2) {
if ((mostRecentPrime + add) % ind == 0) {
hasBeenBroken = true;
break;
}
}
if (!hasBeenBroken) {
mostRecentPrime += add;
break;
}
}
}
return (primes == 1) ? 2 : mostRecentPrime;
}```
Thanks!