Created
July 22, 2016 06:56
-
-
Save LiuuY/bf194005ec65f07797e6e6faeca3de1e to your computer and use it in GitHub Desktop.
A sieve implementation in JavaScript
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
// 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