2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: ae9defd7acaf69703ab432ea
|
2021-01-12 08:18:51 -08:00
|
|
|
title: 找出数字范围内的最小公倍数
|
2018-10-10 18:03:03 -04:00
|
|
|
challengeType: 5
|
2020-09-07 16:10:29 +08:00
|
|
|
forumTopicId: 16075
|
2021-01-13 03:31:00 +01:00
|
|
|
dashedName: smallest-common-multiple
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --description--
|
|
|
|
|
2021-01-12 08:18:51 -08:00
|
|
|
在这道题目中,我们需要写一个函数,它接收一个包含两个数字的数组参数 `arr`;它的返回值为这两个数字范围内所有数字(包含这两个数字)的最小公倍数。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
2020-09-07 16:10:29 +08:00
|
|
|
注意,较小数不一定总是出现在数组的第一个元素。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-01-12 08:18:51 -08:00
|
|
|
比如,传入 `[1, 3]`,那么函数的返回结果应为 1、2、3 的最小公倍数,即为 6。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --hints--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-01-12 08:18:51 -08:00
|
|
|
`smallestCommons([1, 5])` 应返回 a number。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
assert.deepEqual(typeof smallestCommons([1, 5]), 'number');
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-01-12 08:18:51 -08:00
|
|
|
`smallestCommons([1, 5])` 应返回 60。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert.deepEqual(smallestCommons([1, 5]), 60);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
2021-01-12 08:18:51 -08:00
|
|
|
`smallestCommons([5, 1])` 应返回 60。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert.deepEqual(smallestCommons([5, 1]), 60);
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-01-12 08:18:51 -08:00
|
|
|
`smallestCommons([2, 10])` 应返回 2520。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert.deepEqual(smallestCommons([2, 10]), 2520);
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-01-12 08:18:51 -08:00
|
|
|
`smallestCommons([1, 13])` 应返回 360360。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert.deepEqual(smallestCommons([1, 13]), 360360);
|
|
|
|
```
|
|
|
|
|
2021-01-12 08:18:51 -08:00
|
|
|
`smallestCommons([23, 18])` 应返回 6056820。
|
2020-09-07 16:10:29 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
assert.deepEqual(smallestCommons([23, 18]), 6056820);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-08-13 17:24:35 +02:00
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
function smallestCommons(arr) {
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
smallestCommons([1,5]);
|
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 03:31:00 +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);
|
|
|
|
}
|
|
|
|
```
|