Skip to content

Instantly share code, notes, and snippets.

@evanlouie
Last active May 18, 2018 21:40
Show Gist options
  • Save evanlouie/3082b3716e3e0d0ed8d0ac07133170f4 to your computer and use it in GitHub Desktop.
Save evanlouie/3082b3716e3e0d0ed8d0ac07133170f4 to your computer and use it in GitHub Desktop.
/**
* Prime number generator
*/
const Primes = function*(upto: number = Infinity) {
const primesList: number[] = [];
for (let current = 2; current < upto; current++) {
const hasPrimeFactor = Boolean(primesList.find(prime => current % prime === 0));
if (hasPrimeFactor) {
continue;
} else {
primesList.push(current);
yield current;
}
}
};
// Example: log the first 1000000 primes
for (const prime of Primes(1000000)) {
console.log(prime);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment