Number prime test in JavaScript - Stack Overflow

PHOTO EMBED

Sat Dec 04 2021 02:20:39 GMT+0000 (Coordinated Universal Time)

Saved by @tolanisirius #javascript

function isPrime(num) { // returns boolean
  if (num <= 1) return false; // negatives
  if (num % 2 == 0 && num > 2) return false; // even numbers
  const s = Math.sqrt(num); // store the square to loop faster
  for(let i = 3; i <= s; i += 2) { // start from 3, stop at the square, increment in twos
      if(num % i === 0) return false; // modulo shows a divisor was found
  }
  return true;
}
console.log(isPrime(121));
content_copyCOPY

https://stackoverflow.com/questions/40200089/number-prime-test-in-javascript/40200710#40200710?newreg