Last active
August 29, 2015 14:21
-
-
Save elxris/25244fabc6c65851ecbf to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes
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 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