Created
June 23, 2021 13:12
-
-
Save sayanriju/b0318c19dfd1c6693fa33046095351db 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 *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