Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
The Sieve of Eratosthenes in Javascript
function erato(n){
let map = new Map((new Array(n-1).fill(1)).map((n,i)=>[i+2,true]));
for (let i = 2; i <= Math.sqrt(n);i++){
if (!map.get(i)) continue
for (let j = i*2; j <= n; j += i) map.set(j,false)
}
return Array.from(map).filter(([n,b])=>b).map(([n])=>n);
}
console.log(erato(100))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.