Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sayanriju/b0318c19dfd1c6693fa33046095351db to your computer and use it in GitHub Desktop.
Save sayanriju/b0318c19dfd1c6693fa33046095351db to your computer and use it in GitHub Desktop.
function *primeGenerator() {
let knownPrimes = [2]
const isPrime = (num) => knownPrimes.find(p => num % p === 0) === undefined
let n = 2
while (true) {
if (isPrime(n)) {
knownPrimes.push(n)
yield n
}
n++
}
}
const primes = primeGenerator()
console.time("Time Taken ")
for(let i = 0; i<2000; i++) console.log(`#${i+1} --> ${primes.next().value}`);
console.log("--------------------");
console.timeEnd("Time Taken ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment