我必须找到所有数字 1 到 20 的 lcm,然后我想出了这个代码。它虽然效率低下,但也给出了比正确答案多 2 的答案。谁能告诉我为什么?
//variable used to shut off the while loop.
divisable = false;
var i = 2;
//function to prove if the number is a lcm
var isDivisable = function(number){
for(var i = 2; i<=20; i++){
if(number%i !== 0){
divisable = false;
// added the break to shut down the function,
// as no other loops need to be counted
break;
}else{
divisable = true;
}
}
};
while(!divisable){
isDivisable(i);
i+=2;
}