Skip to content

Instantly share code, notes, and snippets.

@nifl
Created October 26, 2012 22:43
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 nifl/3961994 to your computer and use it in GitHub Desktop.
Save nifl/3961994 to your computer and use it in GitHub Desktop.
Sieve Of Eratosthenes
function sieve(max) {
var D = [], primes = [];
for (var q=2; q<max; q++) {
if (D[q]) {
for (var i=0; i<D[q].length; i++) {
var p = D[q][i];
if (D[p+q]) D[p+q].push(p);
else D[p+q]=[p];
}
delete D[q];
} else {
primes.push(q);
if (q*q<max) D[q*q]=[q];
}
}
return primes;
}
sieve(100)
@nifl
Copy link
Author

nifl commented Oct 26, 2012

Returns prime numbers.

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