--- id: 5a23c84252665b21eecc7edf title: Least common multiple challengeType: 5 --- ## Description
The least common multiple of 12 and 18 is 36, because 12 is a factor (12 × 3 = 36), and 18 is a factor (18 × 2 = 36), and there is no positive integer less than 36 that has both factors. As a special case, if either m or n is zero, then the least common multiple is zero. One way to calculate the least common multiple is to iterate all the multiples of m, until you find one that is also a multiple of n. If you already have gcd for greatest common divisor, then this formula calculates lcm. \( \operatorname{lcm}(m, n) = \frac{|m \times n|}{\operatorname{gcd}(m, n)} \)
## Instructions
Compute the least common multiple of an array of intergers. Given m and n, the least common multiple is the smallest positive integer that has both m and n as factors.
## Tests
``` yml tests: - text: LCM should be a function. testString: assert(typeof LCM == 'function', 'LCM should be a function.'); - text: LCM([2, 4, 8]) should return a number. testString: assert(typeof LCM([2, 4, 8]) == 'number', 'LCM([2, 4, 8]) should return a number.'); - text: LCM([2, 4, 8]) should return 8. testString: assert.equal(LCM([2, 4, 8]), 8, 'LCM([2, 4, 8]) should return 8.'); - text: LCM([4, 8, 12]) should return 24. testString: assert.equal(LCM([4, 8, 12]), 24, 'LCM([4, 8, 12]) should return 24.'); - text: LCM([3, 4, 5, 12, 40]) should return 120. testString: assert.equal(LCM([3, 4, 5, 12, 40]), 120, 'LCM([3, 4, 5, 12, 40]) should return 120.'); - text: LCM([11, 33, 90]) should return 990. testString: assert.equal(LCM([11, 33, 90]), 990, 'LCM([11, 33, 90]) should return 990.'); - text: LCM([-50, 25, -45, -18, 90, 447]) should return 67050. testString: assert.equal(LCM([-50, 25, -45, -18, 90, 447]), 67050, 'LCM([-50, 25, -45, -18, 90, 447]) should return 67050.'); ```
## Challenge Seed
```js function LCM(A) { // Good luck! } ```
## Solution
```js function LCM (A) { var n = A.length, a = Math.abs(A[0]); for (var i = 1; i < n; i++) { var b = Math.abs(A[i]), c = a; while (a && b){ a > b ? a %= b : b %= a; } a = Math.abs(c*A[i])/(a+b); } return a; } ```