Skip to content

Instantly share code, notes, and snippets.

@HakurouKen
Created October 17, 2014 03:50
Show Gist options
  • Save HakurouKen/09ed76ce1b891a48fc1e to your computer and use it in GitHub Desktop.
Save HakurouKen/09ed76ce1b891a48fc1e to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes Javascript version
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