mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-02-25 23:01:26 -05:00
1.3 KiB
1.3 KiB
id, title, challengeType, forumTopicId, dashedName
| id | title | challengeType | forumTopicId | dashedName |
|---|---|---|---|---|
| 5900f3711000cf542c50fe84 | Problem 5: Smallest multiple | 1 | 302160 | problem-5-smallest-multiple |
--description--
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
能被从 1 到 n 的所有数整除的最小正数是多少?
--hints--
smallestMult(5) 应该返回数字
assert(typeof smallestMult(5) === 'number');
smallestMult(5) 应该返回 60。
assert.strictEqual(smallestMult(5), 60);
smallestMult(7) 应该返回 420。
assert.strictEqual(smallestMult(7), 420);
smallestMult(10) 应返回 2520。
assert.strictEqual(smallestMult(10), 2520);
smallestMult(13) 应返回 360360。
assert.strictEqual(smallestMult(13), 360360);
smallestMult(20) 应该返回 232792560。
assert.strictEqual(smallestMult(20), 232792560);
--seed--
--seed-contents--
function smallestMult(n) {
return true;
}
smallestMult(20);
--solutions--
function smallestMult(n){
function gcd(a, b) {
return b === 0 ? a : gcd(b, a%b); // Euclidean algorithm
}
function lcm(a, b) {
return a * b / gcd(a, b);
}
var result = 1;
for(var i = 2; i <= n; i++) {
result = lcm(result, i);
}
return result;
}