Smallest Common Multiple
What is "Smallest Common Multiple" ? wiki entry about Least Common Multiple標題的Smallest Common Multiple其實就是最小公倍數(Least Common Multiple)- 兩個整數公有的倍數稱為它們的公倍數,其中最小的一個正整數稱為它們兩個的最小公倍數,而最小公倍數在一般我們實際算的時候,通常是先做質因數分解,然後再依數字質因數分解後的式子,找出最小公倍數,Ex: 有3個數x, y, z: x = 2 = 2^1, y = 3 =, z = 4 = 2^2 ⇒ lcm(x, y, z) = 2^(2) * 3
function smallestCommons(arr) {
arr.sort();
var numberset = [];
for(var i=arr[0];i<=arr[1];i++)
{
numberset.push(i);
}
function gcd(x, y)
{
while(y !== 0)
{
var tmp = x % y;
x = y;
y =tmp;
}
return x;
}
function lcm(x, y)
{
return (x * y)/gcd(x,y);
}
var multiple = arr[0];
numberset.forEach(function(emt)
{
multiple = lcm(multiple, emt);
});
return multiple;
}
smallestCommons([1, 13]);
Solution:
P.S. / Reference: Smallest Common Multiple
Greast Common Divisor
How to find the least common multiple of a range of numbers? - stackoverflow