Number is prime

JavaScript, Math, Algorithm · Jan 12, 2021

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

More like this

  • Prime factors of number

    Finds the prime factors of a given number using the trial division algorithm.

    JavaScript, Math · Dec 28, 2020

  • Luhn check

    Implements the Luhn Algorithm, used to validate a variety of identification numbers.

    JavaScript, Math · Jan 30, 2022

  • Primes up to given number

    Generates primes up to a given number, using the Sieve of Eratosthenes.

    JavaScript, Math · Dec 28, 2020