https://learn.freecodecamp.org/coding-interview-prep/project-euler/problem-7-10001st-prime
For this problem, my code won’t pass the last test due to inefficiency but having a hard time finding ways to improve it. As you see in my code, I’m not looping through all the numbers as I start at the square root of each number being analyzed.
function nthPrime(n) {
let count = 0;
let prime;
let j;
let i = 2;
while (count < n) {
for (j = Math.ceil( Math.sqrt(i) ) ; j > 1; j--) {
if (i !== j && i % j == 0) break;
}
if (j == 1) {
count++;
prime = i;
}
i++;
}
return prime;
}
nthPrime(10001);