Project Euler

Solutions in JavaScript

Problem
7
Find the 10001st prime.

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10001st prime number?

Solution


function prime(index) {
    var i, primes = [2, 3], n = 5;

    function isPrime(n) {
        var i = 1, p = primes[i],
            limit = Math.ceil(Math.sqrt(n));
        while (p <= limit) {
            if (n % p === 0) {
                return false;
            }
            i += 1;
            p = primes[i];
        }
        return true;
    }

    for (i = 2; i <= index; i += 1) {
        while (!isPrime(n)) {
            n += 2;
        }
        primes.push(n);
        n += 2;
    }
    return primes[index - 1];
}

console.log(prime(1e4 + 1));

1 2 3 4 5 6 7 8

© 2012 Giulio Piancastelli
Suggestions? Issues? Comments? I’d love to hear you!