Skip to content

Instantly share code, notes, and snippets.

@mahata
Created January 12, 2023 01:48
Show Gist options
  • Save mahata/2844a248a7cc5f667b3783f5ef87f03a to your computer and use it in GitHub Desktop.
Save mahata/2844a248a7cc5f667b3783f5ef87f03a to your computer and use it in GitHub Desktop.
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