Skip to content

Instantly share code, notes, and snippets.

@thejsj
Created January 23, 2017 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thejsj/e780911512e475295ca904a2cf1e86bd to your computer and use it in GitHub Desktop.
Save thejsj/e780911512e475295ca904a2cf1e86bd to your computer and use it in GitHub Desktop.
Get all prime number up to N
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