McInkay
8/13/2015 - 9:02 AM

Prime number generator using ES6

Prime number generator using ES6

var startTime = Date.now(),
    primeList = [],
    time;
    
for (let i of primeGenerator({max: 10000})) {
  primeList.push(i);
}

time = (Date.now() - startTime)/1000;
console.log(`${primeList.length} primes in ${time} seconds`);
console.log(primeList);
function* primeGenerator({max = 1000}) {
  var sieve = [], current = 1;
  while(++current < max) {
    if (!sieve[current]) {
      for (var j = current << 1; j < max; j += current) {
        sieve[j] = true;
      }
      yield current;
    }
  }
}