求出最小公倍数
2018-09-25 本文已影响0人
可乐杯杯hh
我使用更相减损法完成了这个挑战,真是可怕又冗余的代码行数。
function smallestCommons(arr)
{
function minCommon(a,b)
{
let x = maxYue(a,b);
return a*b/x;
}
function maxYue(a,b) {
if (a%2===0 && b%2===0)
{
a = a/2;
b = b/2;
while (a !== b)//10,15
{
if(a>b)
{
a = a-b;//a=5,b=5;a=6,b=2;a=2,
}
else
{
b = b-a;//b = 5,a = 10;
}
}
return a*2;
}
while (a !== b)//10,15
{
if(a>b)
{
a = a-b;//a=5,b=5;a=6,b=2;a=2,
}
else
{
b = b-a;//b = 5,a = 10;
}
}
return a;
}
let newArr = [];
for (let i = arr[0];i <= arr[1]; i++)
{
newArr.push(i);
}
let temp = minCommon(newArr[0],newArr[1]);
for (let i = newArr[2]; i <= newArr[newArr.length-1]; i++)
{
temp = minCommon(temp,i);
}
return temp;
}
console.log(smallestCommons([1,5]));