Created
January 23, 2017 00:59
-
-
Save thejsj/e780911512e475295ca904a2cf1e86bd to your computer and use it in GitHub Desktop.
Get all prime number up to N
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 sieve (n) { | |
let all = [...Array(n).keys()].map(x => true) | |
for (let i = 2; i <= Math.sqrt(n); i += 1) { | |
if (all[i]) { | |
for (let j = i * i; j < n; j += i) { | |
all[j] = false | |
} | |
} | |
} | |
return all.map((x, i) => { | |
if (x && i > 1) return i | |
}).filter(x => !!x) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment