Merge pull request #265 from fejes713/master

[FEATURE] add isPrime
This commit is contained in:
Angelos Chalaris
2017-12-20 11:12:26 +02:00
committed by GitHub
4 changed files with 47 additions and 0 deletions

View File

@ -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.

View File

@ -112,6 +112,7 @@
<a class="sublink-1" href="#hammingdistance">hammingDistance</a>
<a class="sublink-1" href="#isdivisible">isDivisible</a>
<a class="sublink-1" href="#iseven">isEven</a>
<a class="sublink-1" href="#isprime">isPrime</a>
<a class="sublink-1" href="#lcm">lcm</a>
<a class="sublink-1" href="#median">median</a>
<a class="sublink-1" href="#palindrome">palindrome</a>
@ -761,6 +762,17 @@ Returns <code>true</code> if the number is even, <code>false</code> if the numbe
<pre><code class="language-js">const isEven = num =&gt; num % 2 === 0;
// isEven(3) -&gt; false
</code></pre>
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="isprime">isPrime</h3></div><div class="section double-padded">
<p>Checks if the provided integer is a prime number.</p>
<p>Returns <code>false</code> if the provided number has positive divisors other than 1 and itself or if the number itself is less than 2.</p>
<pre><code class="language-js">const isPrime = num =&gt; {
for (var i = 2; i &lt; num; i++) if (num % i == 0) return false;
return num &gt;= 2;
}
// isPrime(11) -&gt; true
// isPrime(12) -&gt; false
// isPrime(1) -&gt; false
</code></pre>
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="lcm">lcm</h3></div><div class="section double-padded">
<p>Returns the least common multiple of two numbers.</p>
<p>Use the greatest common divisor (GCD) formula and <code>Math.abs()</code> to determine the least common multiple.

15
snippets/isPrime.md Normal file
View File

@ -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
```

View File

@ -56,6 +56,7 @@ isDivisible:math
isEven:math
isFunction:utility
isNumber:utility
isPrime:math
isString:utility
isSymbol:utility
JSONToDate:date