Skip to content

Instantly share code, notes, and snippets.

@wlib
Last active March 12, 2023 01:54
Show Gist options
  • Save wlib/d05580799263664731af42f37eb2e934 to your computer and use it in GitHub Desktop.
Save wlib/d05580799263664731af42f37eb2e934 to your computer and use it in GitHub Desktop.
Javascript prime number generator function
function * primes(max = 1_000) {
const isComposite = []
for (let i = 2; i <= max; i++) if (!isComposite[i]) {
yield i
for (let j = i**2; j <= max; j += i)
isComposite[j] = true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment