Skip to content

Instantly share code, notes, and snippets.

@shabin-slr
Created January 15, 2019 13:26
Show Gist options
  • Save shabin-slr/c1b842547c2a1f70f09ac64e1cf135f6 to your computer and use it in GitHub Desktop.
Save shabin-slr/c1b842547c2a1f70f09ac64e1cf135f6 to your computer and use it in GitHub Desktop.
Simple implementation of Sieve of Eratosthenes (eSieve) in JavaScript
// https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
var eSieve = function(limit){
var numbers = [];
for(let i = 2; i <= limit; ++i){
numbers.push({
value : i
});
};
//numbers[0].isPrime = true;
for(i = 0; i<numbers.length; ++i){
if(numbers[i].isPrime !== false){
for(let j = i + numbers[i].value; j < numbers.length; j+= numbers[i].value){
numbers[j].isPrime = false;
}
}
}
return numbers.filter(x=>x.isPrime !== false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment