582 B
582 B
title, type, language, tags, cover, dateModified
| title | type | language | tags | cover | dateModified | ||
|---|---|---|---|---|---|---|---|
| Number is prime | snippet | javascript |
|
thread | 2021-01-12T19:36:36+02:00 |
Checks if the provided integer is a prime number.
- Check numbers from
2to the square root of the given number. - Return
falseif any of them divides the given number, else returntrue, unless the number is less than2.
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