From c4eb961b5c3d48a515f37521287cb0fe25d364bb Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Fri, 22 Dec 2017 10:31:50 +0200 Subject: [PATCH] Resolves #297 --- snippets/isPrime.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/snippets/isPrime.md b/snippets/isPrime.md index 0a2157e2f..6f77ebd6c 100644 --- a/snippets/isPrime.md +++ b/snippets/isPrime.md @@ -2,11 +2,13 @@ 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. +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`. ```js const isPrime = num => { - for (var i = 2; i * i <= num; i++) if (num % i == 0) return false; + const boundary = Math.floor(Math.sqrt(num)); + for (var i = 2; i * i <= boundary; i++) if (num % i == 0) return false; return num >= 2; }; // isPrime(11) -> true