Skip to content

Instantly share code, notes, and snippets.

@robbywashere-zz
Created March 11, 2019 01:45
Show Gist options
  • Save robbywashere-zz/798eae567beb32ab4c9ae55f78e55eb5 to your computer and use it in GitHub Desktop.
Save robbywashere-zz/798eae567beb32ab4c9ae55f78e55eb5 to your computer and use it in GitHub Desktop.
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