Created
March 11, 2019 01:45
-
-
Save robbywashere-zz/798eae567beb32ab4c9ae55f78e55eb5 to your computer and use it in GitHub Desktop.
The Sieve of Eratosthenes 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
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