2019-03-21 11:52:35 +05:30
---
id: 5a23c84252665b21eecc7edf
title: Least common multiple
challengeType: 5
2019-08-05 09:17:33 -07:00
forumTopicId: 302301
2019-03-21 11:52:35 +05:30
---
## Description
2020-03-30 11:23:18 -05:00
2019-03-21 11:52:35 +05:30
< section id = 'description' >
2019-07-18 17:32:12 +02:00
2020-03-30 11:23:18 -05:00
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 < i > m< / i > or < i > n< / i > is zero, then the least common multiple is zero.
One way to calculate the least common multiple is to iterate all the multiples of < i > m< / i > , until you find one that is also a multiple of < i > n< / i > .
If you already have < i > gcd< / i > for < a href = "https://rosettacode.org/wiki/greatest common divisor" target = "_blank" > greatest common divisor< / a > , then this formula calculates < i > lcm< / i > .
2019-03-21 11:52:35 +05:30
\( \operatorname{lcm}(m, n) = \frac{|m \times n|}{\operatorname{gcd}(m, n)} \)
2020-03-30 11:23:18 -05:00
2019-03-21 11:52:35 +05:30
< / section >
## Instructions
2020-03-30 11:23:18 -05:00
2019-03-21 11:52:35 +05:30
< section id = 'instructions' >
2019-07-18 17:32:12 +02:00
2020-02-08 13:29:10 -05:00
Compute the least common multiple of an array of integers.
2020-03-30 11:23:18 -05:00
Given < i > m< / i > and < i > n< / i > , the least common multiple is the smallest positive integer that has both < i > m< / i > and < i > n< / i > as factors.
2019-03-21 11:52:35 +05:30
< / section >
## Tests
2020-03-30 11:23:18 -05:00
2019-03-21 11:52:35 +05:30
< section id = 'tests' >
2020-03-30 11:23:18 -05:00
```yml
2019-03-21 11:52:35 +05:30
tests:
- text: < code > LCM</ code > should be a function.
2020-03-30 11:23:18 -05:00
testString: assert(typeof LCM == 'function');
2019-03-21 11:52:35 +05:30
- text: < code > LCM([2, 4, 8])</ code > should return a number.
2020-03-30 11:23:18 -05:00
testString: assert(typeof LCM([2, 4, 8]) == 'number');
2019-03-21 11:52:35 +05:30
- text: < code > LCM([2, 4, 8])</ code > should return < code > 8</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.equal(LCM([2, 4, 8]), 8);
2019-03-21 11:52:35 +05:30
- text: < code > LCM([4, 8, 12])</ code > should return < code > 24</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.equal(LCM([4, 8, 12]), 24);
2019-03-21 11:52:35 +05:30
- text: < code > LCM([3, 4, 5, 12, 40])</ code > should return < code > 120</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.equal(LCM([3, 4, 5, 12, 40]), 120);
2019-03-21 11:52:35 +05:30
- text: < code > LCM([11, 33, 90])</ code > should return < code > 990</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.equal(LCM([11, 33, 90]), 990);
2019-03-21 11:52:35 +05:30
- text: < code > LCM([-50, 25, -45, -18, 90, 447])</ code > should return < code > 67050</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.equal(LCM([-50, 25, -45, -18, 90, 447]), 67050);
2019-03-21 11:52:35 +05:30
```
< / section >
## Challenge Seed
2020-03-30 11:23:18 -05:00
2019-03-21 11:52:35 +05:30
< section id = 'challengeSeed' >
2019-07-18 17:32:12 +02:00
2019-03-21 11:52:35 +05:30
< div id = 'js-seed' >
```js
function LCM(A) {
// Good luck!
}
```
< / div >
< / section >
## Solution
2020-03-30 11:23:18 -05:00
2019-03-21 11:52:35 +05:30
< section id = 'solution' >
```js
2020-03-30 11:23:18 -05:00
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;
2019-03-21 11:52:35 +05:30
}
```
2019-07-18 17:32:12 +02:00
< / section >