Files
30-seconds-of-code/snippets/isPrime.md
Isabelle Viktoria Maciohsek 02faddb6a8 Tags housekeeping
2020-10-18 13:49:49 +03:00

482 B

title, tags
title tags
isPrime math,beginner

Checks if the provided integer is a prime number.

  • Check numbers from 2 to the square root of the given number.
  • Return false if any of them divides the given number, else return true, unless the number is less than 2.
const isPrime = num => {
  const boundary = Math.floor(Math.sqrt(num));
  for (var i = 2; i <= boundary; i++) if (num % i === 0) return false;
  return num >= 2;
};
isPrime(11); // true