Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Prime number calculator used as part of a debugging tutorial on talktotheduck.dev
const MAX_NUMBER = 10 ** 9;
let cnt = 0
function isPrime(num) {
if (num === 2) {
return true;
}
if (num < 2 || num % 2 === 0) {
return false;
}
for (let i = 3; i * i <= num; i += 2) {
if (num % i === 0) {
return false;
}
}
return true;
}
function countPrimes(i) {
if (isPrime(i)) {
cnt++;
}
if (i < MAX_NUMBER) {
setTimeout(() => countPrimes(++i));
} else {
console.log("Total number of primes: " + cnt);
}
}
console.log("Starting to count prime numbers...");
setTimeout(() => countPrimes(2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment