Skip to content

Instantly share code, notes, and snippets.

@elxris
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elxris/25244fabc6c65851ecbf to your computer and use it in GitHub Desktop.
Save elxris/25244fabc6c65851ecbf to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes
var input = process.env.INPUT || 2;
var sieve = function(upperBound) {
var boolArray = new ArrayBuffer(upperBound);
var i, j;
for (i = 2; i < (Math.sqrt(upperBound))|0; i++) {
if (boolArray[i] === 0) {
for (j = 0; (i*2 + j*i) < upperBound; j++) {
boolArray[(i*2 + j*i)] = 1;
}
}
}
// for (i = 2; i < upperBound; i++) {
// if (boolArray[i] === 0) {
// process.stdout.write(i+" ");
// }
// }
// process.stdout.write("\n");
};
console.time("sieve");
sieve(input);
console.timeEnd("sieve");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment