diff --git a/README.md b/README.md index bce4069d1..715b1a3c9 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,7 @@ * [`hammingDistance`](#hammingdistance) * [`isDivisible`](#isdivisible) * [`isEven`](#iseven) +* [`isPrime`](#isprime) * [`lcm`](#lcm) * [`median`](#median) * [`palindrome`](#palindrome) @@ -1216,6 +1217,24 @@ const isEven = num => num % 2 === 0; [⬆ back to top](#table-of-contents) +### 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 < num; i++) if (num % i == 0) return false; + return num >= 2; +} +// isPrime(11) -> true +// isPrime(12) -> false +// isPrime(1) -> false +``` + +[⬆ back to top](#table-of-contents) + ### lcm Returns the least common multiple of two numbers. diff --git a/docs/index.html b/docs/index.html index 38b5a08ac..d0bd32993 100644 --- a/docs/index.html +++ b/docs/index.html @@ -112,6 +112,7 @@ hammingDistance isDivisible isEven +isPrime lcm median palindrome @@ -761,6 +762,17 @@ Returns true if the number is even, false if the numbe
const isEven = num => num % 2 === 0;
 // isEven(3) -> false
 
+

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.

+
const isPrime = num => {
+  for (var i = 2; i < num; i++) if (num % i == 0) return false;
+  return num >= 2;
+}
+// isPrime(11) -> true
+// isPrime(12) -> false
+// isPrime(1) -> false
+

lcm

Returns the least common multiple of two numbers.

Use the greatest common divisor (GCD) formula and Math.abs() to determine the least common multiple. diff --git a/snippets/isPrime.md b/snippets/isPrime.md new file mode 100644 index 000000000..cc1f69b14 --- /dev/null +++ b/snippets/isPrime.md @@ -0,0 +1,15 @@ +### 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 < num; i++) if (num % i == 0) return false; + return num >= 2; +} +// isPrime(11) -> true +// isPrime(12) -> false +// isPrime(1) -> false +``` diff --git a/tag_database b/tag_database index 45af1e960..5eb4c145c 100644 --- a/tag_database +++ b/tag_database @@ -56,6 +56,7 @@ isDivisible:math isEven:math isFunction:utility isNumber:utility +isPrime:math isString:utility isSymbol:utility JSONToDate:date