JavaScript Implementation of Problem 7

View source code here on GitHub!

Includes

Problem Solution

p0007()

Project Euler Problem 7

Finally ported the prime generator to js

Problem:

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 10 001st prime number?

Returns:

number --

 1/**
 2 * Project Euler Problem 7
 3 *
 4 * Finally ported the prime generator to js
 5 *
 6 * Problem:
 7 *
 8 * By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that
 9 * the 6th prime is 13.
10 *
11 * What is the 10 001st prime number?
12 *
13 * @return {number}
14 */
15exports.p0007 = function() {
16    const pgen = primes.modifiedEratosthenes();
17    for (let idx = 1; ; idx++) {
18        const num = pgen.next().value;
19        if (idx === 10001) {
20            return num;
21        }
22    }
23};
24
25const primes = require('./lib/primes.js');

Tags: prime-number, js-iterator