DEV Community

loading...

Discussion on: Project Euler #7 - 10001st prime

Collapse
crownedprinz profile image
Ademola John

Here is a javascript solution


function nthPrime(n) {
    let primes = [2];
    let higherDivisorLimit;
    let isPrime;
    for(let i = 3; primes.length < n; i+=2) {
      higherDivisorLimit = Math.sqrt(i) + 1;
      isPrime = true;
      for(let j = 0; primes[j] < higherDivisorLimit; j++) {
        if(i % primes[j] === 0) {
          isPrime = false;
          break;
        }
      }
      if(isPrime) primes.push(i);
    }
    return primes[primes.length - 1];
}

console.log(nthPrime(10001));