Skip to content

Instantly share code, notes, and snippets.

@alexbaldwin
Created July 8, 2012 23:27
Show Gist options
  • Save alexbaldwin/3073413 to your computer and use it in GitHub Desktop.
Save alexbaldwin/3073413 to your computer and use it in GitHub Desktop.
Sieve
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));
@alexbaldwin
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment