Created
January 12, 2023 01:48
-
-
Save mahata/2844a248a7cc5f667b3783f5ef87f03a to your computer and use it in GitHub Desktop.
This file contains 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
function *createPrimeGenerator() { | |
const primes = new Set<number>() | |
let current = 2 | |
while (true) { | |
while (true) { | |
let isPrime = true | |
for (const prime of primes) { | |
if (current % prime === 0) { | |
isPrime = false | |
break | |
} | |
} | |
if (!isPrime) { | |
current++; | |
continue | |
} | |
primes.add(current) | |
break | |
} | |
yield current++ | |
} | |
} | |
const primeGenerator = createPrimeGenerator() | |
for (let i = 0; i < 100; i++) { | |
console.log(primeGenerator.next()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment