Skip to content

Instantly share code, notes, and snippets.

@tahoemph
Created July 18, 2021 20:44
Show Gist options
  • Save tahoemph/c2ff885f222fc5ecb04aabfbe0ca6917 to your computer and use it in GitHub Desktop.
Save tahoemph/c2ff885f222fc5ecb04aabfbe0ca6917 to your computer and use it in GitHub Desktop.
const list_of_primes = [2, 3, 5];
function* prime_generator() {
for (prime of list_of_primes) {
yield prime;
}
for(let next_possible_prime = list_of_primes.slice(-1)[0] + 2;;next_possible_prime += 2) {
const max_check = Math.ceil(Math.sqrt(next_possible_prime));
for (known_prime of list_of_primes) {
if (known_prime > max_check) {
list_of_primes.push(next_possible_prime);
yield next_possible_prime;
break;
}
if (next_possible_prime % known_prime == 0) {
break;
}
}
}
}
const primes = prime_generator();
for (const prime of primes) {
console.log(prime)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment