Created
October 26, 2012 22:43
-
-
Save nifl/3961994 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
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Returns prime numbers.