function isPrime(n) { if (n < 2) return false; // do an early exit for even numbers if (n == 2) return true; if (n % 2 == 0) return false; // we only have to check odd numbers, starting with 3 for (var x = 3; x <= Math.floor(Math.sqrt(n)); x += 2) if (n % x == 0) return false; return true; }
- function isPrime(n) {
if (n < 2) return false;for (var x = 2; x <= Math.floor(Math.sqrt(n)); x++) {if (n % x == 0) return false;}- if (n < 2)
- return false;
- // do an early exit for even numbers
- if (n == 2)
- return true;
- if (n % 2 == 0)
- return false;
- // we only have to check odd numbers, starting with 3
- for (var x = 3; x <= Math.floor(Math.sqrt(n)); x += 2)
- if (n % x == 0)
- return false;
- return true;
- }