Files
30-seconds-of-code/snippets/isPrime.md
Yuchen Zhang 544d69afa1 Improve isPrime to be O(sqrt(n)) (#298)
* improve isPrime to be O(sqrt(n))
2017-12-21 23:31:51 +01:00

15 lines
363 B
Markdown

### isPrime
Checks if the provided integer is a prime number.
Returns `false` if the provided number has positive divisors other than 1 and itself or if the number itself is less than 2.
```js
const isPrime = num => {
for (var i = 2; i * i <= num; i++) if (num % i == 0) return false;
return num >= 2;
};
// isPrime(11) -> true
// isPrime(12) -> false
```