how to check if a number is prime in js code example
Example 1: javascript find a prime number
function isPrime(num) {
if(num < 2) return false;
for (let k = 2; k < num; k++){
if( num % k == 0){
return false;
}
}
return true;
}
Example 2: Javascript prime number check
function is_prime (n) {
if (n < 3) return n > 1;
else if (n % 2 === 0 || n % 3 === 0) return false;
else if (n < 25) return true;
let i = 5;
while (i * i <= n ) {
if (n % i === 0 || n % (i + 2) === 0) return false;
i += 6;
}
return true;
}