Menu

gitpiper

isPrime javascript Code Snippet in 2024

mathalgorithmbeginner

Last Updated: 19 September 2024

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

javascript snippet similar to isPrime For You in September 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️