Files
30-seconds-of-code/snippets/isPrime.md
Isabelle Viktoria Maciohsek 27c168ce55 Bake date into snippets
2021-06-13 13:55:00 +03:00

568 B

title, tags, firstSeen, lastUpdated
title tags firstSeen lastUpdated
isPrime math,algorithm,beginner 2017-12-19T22:35:56+02:00 2021-01-12T19:36:36+02:00

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 (let i = 2; i <= boundary; i++) if (num % i === 0) return false;
  return num >= 2;
};
isPrime(11); // true