Last active
May 18, 2018 21:40
-
-
Save evanlouie/3082b3716e3e0d0ed8d0ac07133170f4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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