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
?
smallestMult(5)
should return 60.
smallestMult(10)
should return 2520.
smallestMult(13)
should return 360360.
smallestMult(20)
should return 232792560.
function smallestMult(n) {
let result = 1;
for(let i=1;i<=n;i++){
result = lcm(result,i);
}
return result;
}
function gcd(a,b){
if(b===0){
return a;
}else {
return gcd(b,a%b);
}
}
function lcm(a,b){
return Math.abs(a*b)/gcd(a,b);
}