* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
1.7 KiB
1.7 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
ae9defd7acaf69703ab432ea | 找出数字范围内的最小公倍数 | 5 | 16075 | smallest-common-multiple |
--description--
在这道题目中,我们需要写一个函数,它接收一个包含两个数字的数组参数 arr
;它的返回值为这两个数字范围内所有数字(包含这两个数字)的最小公倍数。
注意,较小数不一定总是出现在数组的第一个元素。
比如,传入 [1, 3]
,那么函数的返回结果应为 1、2、3 的最小公倍数,即为 6。
--hints--
smallestCommons([1, 5])
应返回 a number。
assert.deepEqual(typeof smallestCommons([1, 5]), 'number');
smallestCommons([1, 5])
应返回 60。
assert.deepEqual(smallestCommons([1, 5]), 60);
smallestCommons([5, 1])
应返回 60。
assert.deepEqual(smallestCommons([5, 1]), 60);
smallestCommons([2, 10])
应返回 2520。
assert.deepEqual(smallestCommons([2, 10]), 2520);
smallestCommons([1, 13])
应返回 360360。
assert.deepEqual(smallestCommons([1, 13]), 360360);
smallestCommons([23, 18])
应返回 6056820。
assert.deepEqual(smallestCommons([23, 18]), 6056820);
--seed--
--seed-contents--
function smallestCommons(arr) {
return arr;
}
smallestCommons([1,5]);
--solutions--
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);
}