Created
October 17, 2014 03:50
-
-
Save HakurouKen/09ed76ce1b891a48fc1e to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes Javascript version
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
var sieve = function(num) { | |
var arr = [], | |
primes = [], | |
i = 0, | |
curPrime; | |
num = parseInt(num); | |
if (isNaN(num) || num < 2) { | |
return primes; | |
} | |
for (; i < num - 1; i++) { | |
arr[i] = i + 2; | |
} | |
while (arr[0]) { | |
primes.push((curPrime = arr[0])); | |
arr = arr.slice(1).filter(function(i) { | |
return i % curPrime !== 0; | |
}); | |
} | |
return primes; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment