Travis build: 626
This commit is contained in:
@ -2546,17 +2546,16 @@ isPrime(12); // false
|
||||
|
||||
### lcm
|
||||
|
||||
Returns the least common multiple of two or numbers/arrays.
|
||||
Returns the least common multiple of two or more numbers/arrays.
|
||||
|
||||
Use the greatest common divisor (GCD) formula and `Math.abs()` to determine the least common multiple.
|
||||
The GCD formula uses recursion.
|
||||
|
||||
```js
|
||||
const lcm = (...arr) => {
|
||||
let data = [].concat(...arr);
|
||||
const gcd = (x, y) => (!y ? x : gcd(y, x % y));
|
||||
const helperLcm = (x, y) => x * y / gcd(x, y);
|
||||
return arr.reduce((a, b) => helperLcm(a, b));
|
||||
const _lcm = (x, y) => x * y / gcd(x, y);
|
||||
return [].concat(...arr).reduce((a, b) => _lcm(a, b));
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
@ -482,11 +482,10 @@ isArmstrongNumber(56); // false
|
||||
};
|
||||
</code></pre><pre><code class="language-js">isPrime(11); // true
|
||||
isPrime(12); // 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 or numbers/arrays.</p><p>Use the greatest common divisor (GCD) formula and <code>Math.abs()</code> to determine the least common multiple. The GCD formula uses recursion.</p><pre><code class="language-js">const lcm = (...arr) => {
|
||||
let data = [].concat(...arr);
|
||||
</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 or more numbers/arrays.</p><p>Use the greatest common divisor (GCD) formula and <code>Math.abs()</code> to determine the least common multiple. The GCD formula uses recursion.</p><pre><code class="language-js">const lcm = (...arr) => {
|
||||
const gcd = (x, y) => (!y ? x : gcd(y, x % y));
|
||||
const helperLcm = (x, y) => x * y / gcd(x, y);
|
||||
return arr.reduce((a, b) => helperLcm(a, b));
|
||||
const _lcm = (x, y) => x * y / gcd(x, y);
|
||||
return [].concat(...arr).reduce((a, b) => _lcm(a, b));
|
||||
};
|
||||
</code></pre><pre><code class="language-js">lcm(12, 7); // 84
|
||||
lcm([1, 3, 4], 5); // 60
|
||||
|
||||
Reference in New Issue
Block a user