Created
July 8, 2012 23:27
-
-
Save alexbaldwin/3073413 to your computer and use it in GitHub Desktop.
Sieve
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 computePrimes = function(limit) { | |
var sieve = []; | |
for (var i = 2; i < limit; i++) { | |
if (sieve[i] === false) { | |
continue; | |
} | |
for (var j = 2; j * i < limit; j++) { | |
sieve[j * i] = false; | |
} | |
} | |
var primes = []; | |
for (var i = 2; i < limit; i++) { | |
if (sieve[i] !== false) { | |
primes.push(i); | |
} | |
} | |
return primes; | |
}; | |
console.log(computePrimes(100)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes