2018-09-30 23:01:58 +01:00
---
id: ae9defd7acaf69703ab432ea
title: Smallest Common Multiple
challengeType: 5
2019-07-31 11:32:23 -07:00
forumTopicId: 16075
2021-01-13 03:31:00 +01:00
dashedName: smallest-common-multiple
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2018-09-30 23:01:58 +01:00
Find the smallest common multiple of the provided parameters that can be evenly divided by both, as well as by all sequential numbers in the range between these parameters.
2020-11-27 19:02:05 +01:00
2018-09-30 23:01:58 +01:00
The range will be an array of two numbers that will not necessarily be in numerical order.
2020-11-27 19:02:05 +01:00
For example, if given 1 and 3, find the smallest common multiple of both 1 and 3 that is also evenly divisible by all numbers *between* 1 and 3. The answer here would be 6.
# --hints--
`smallestCommons([1, 5])` should return a number.
```js
assert.deepEqual(typeof smallestCommons([1, 5]), 'number');
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
`smallestCommons([1, 5])` should return 60.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert.deepEqual(smallestCommons([1, 5]), 60);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`smallestCommons([5, 1])` should return 60.
2018-09-30 23:01:58 +01:00
```js
2020-11-27 19:02:05 +01:00
assert.deepEqual(smallestCommons([5, 1]), 60);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`smallestCommons([2, 10])` should return 2520.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert.deepEqual(smallestCommons([2, 10]), 2520);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
`smallestCommons([1, 13])` should return 360360.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert.deepEqual(smallestCommons([1, 13]), 360360);
```
`smallestCommons([23, 18])` should return 6056820.
```js
assert.deepEqual(smallestCommons([23, 18]), 6056820);
```
# --seed--
## --seed-contents--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
function smallestCommons(arr) {
return arr;
}
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
smallestCommons([1,5]);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
function gcd(a, b) {
while (b !== 0) {
a = [b, b = a % b][0];
}
return a;
}
function lcm(a, b) {
return (a * b) / gcd(a, b);
}
function smallestCommons(arr) {
arr.sort(function(a,b) {return a-b;});
var rng = [];
for (var i = arr[0]; i < = arr[1]; i++) {
rng.push(i);
}
return rng.reduce(lcm);
}
```