-
-
Save chrisdickinson/2d371c365f8bb0d9c9ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var SB = require('buffer').SlowBuffer; | |
function runner(cb, arg) { | |
process.nextTick(function() { | |
cb(arg) | |
}) | |
} | |
var iter = 2e4 | |
for(var i = 0; i < iter; ++i) { | |
runner(genPrimes, i) | |
} | |
function genPrimes(max) { | |
var len = (max >>> 3) + 1 | |
, sieve = new SB(len) | |
, primes = [] | |
, cntr | |
, x | |
, j | |
sieve.fill(0xff, 0, len) | |
for(cntr = 0, x = 2; x <= max; ++x) { | |
if(sieve[x >>> 3] & (1 << (x & 7))) { | |
primes[cntr++] = x | |
for(j = 2 * x; j <= max; j += x) { | |
sieve[j >>> 3] &= ~(1 << (j & 7)) | |
} | |
} | |
} | |
return primes | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var SB = require('buffer').SlowBuffer; | |
function runner(cb, arg) { | |
process.nextTick(function() { | |
cb(arg); | |
}); | |
} | |
var iter = 2e4; | |
for (var i = 0; i < iter; i++) { | |
runner(genPrimes, i); | |
} | |
function genPrimes(max) { | |
var primes = []; | |
var len = ((max / 8) >>> 0) + 1; | |
var sieve = new SB(len); | |
sieve.fill(0xff, 0, len); | |
var cntr, x, j; | |
for (cntr = 0, x = 2; x <= max; x++) { | |
if (sieve[(x / 8) >>> 0] & (1 << (x % 8))) { | |
primes[cntr++] = x; | |
for (j = 2 * x; j <= max; j += x) { | |
sieve[(j / 8) >>> 0] &= ~(1 << (j % 8)); | |
} | |
} | |
} | |
return primes; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment