Created
January 15, 2019 13:26
-
-
Save shabin-slr/c1b842547c2a1f70f09ac64e1cf135f6 to your computer and use it in GitHub Desktop.
Simple implementation of Sieve of Eratosthenes (eSieve) 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
// 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