Skip to content

Instantly share code, notes, and snippets.

@mattdiamond
Last active November 26, 2019 21:33
Show Gist options
  • Save mattdiamond/564e701d5ed465db2d0ef08df14ed9fd to your computer and use it in GitHub Desktop.
Save mattdiamond/564e701d5ed465db2d0ef08df14ed9fd to your computer and use it in GitHub Desktop.
function * primes (count = Infinity) {
yield 2;
search:
for (let number = 3, found = 1; found < count; number += 2) {
const root = Math.sqrt(number);
for (let divisor = 3; divisor <= root; divisor += 2) {
if (number % divisor === 0) {
continue search;
}
}
found++;
yield number;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment