Problem 5: Smallest multiple
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to n
?
Solution:
function smallestMult(n) {
let lcm = 1;
for(let i=2;i<=n;i++) {
lcm = LCM(lcm,i)
}
console.log(lcm)
return lcm;
}
function GCD(x,y) {
if(y === 0) return x;
return GCD(y, x%y);
}
function LCM(x,y) {
return x*y/GCD(x,y)
}
smallestMult(10);
Click here to go to the original link of the question.