Skip to content

Instantly share code, notes, and snippets.

@LiuuY
Created July 22, 2016 06:56
Show Gist options
  • Save LiuuY/bf194005ec65f07797e6e6faeca3de1e to your computer and use it in GitHub Desktop.
Save LiuuY/bf194005ec65f07797e6e6faeca3de1e to your computer and use it in GitHub Desktop.
A sieve implementation in JavaScript
// http://stackoverflow.com/a/12287599
function getPrimes(max) {
var sieve = [], i, j, primes = [];
for (i = 2; i <= max; ++i) {
if (!sieve[i]) {
// i has not been marked -- it is prime
primes.push(i);
for (j = i << 1; j <= max; j += i) {
sieve[j] = true;
}
}
}
return primes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment