mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-01-30 15:04:00 -05:00
1.4 KiB
1.4 KiB
id, title, challengeType, forumTopicId, dashedName
| id | title | challengeType | forumTopicId | dashedName |
|---|---|---|---|---|
| 5900f3711000cf542c50fe84 | Problem 5: Kleinstes Vielfaches | 1 | 302160 | problem-5-smallest-multiple |
--description--
2520 ist die kleinste Zahl, die durch jede der Zahlen von 1 bis 10 geteilt werden kann, ohne dass ein Rest übrig bleibt.
Was ist die kleinste positive Zahl, die durch alle Zahlen von 1 bis n gleichmäßig teilbar ist?
--hints--
smallestMult(5) sollte eine Zahl zurückgeben.
assert(typeof smallestMult(5) === 'number');
smallestMult(5) sollte 60 zurückgeben.
assert.strictEqual(smallestMult(5), 60);
smallestMult(7) sollte 420 zurückgeben.
assert.strictEqual(smallestMult(7), 420);
smallestMult(10) sollte 2520 zurückgeben.
assert.strictEqual(smallestMult(10), 2520);
smallestMult(13) sollte 360360 zurückgeben.
assert.strictEqual(smallestMult(13), 360360);
smallestMult(20) sollte 232792560 zurückgeben.
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;
}