求幾個數的最小公倍數就是先求出前兩個數的最小公倍數,而後再把這個最小公倍數跟第三個數放在一塊兒來求最小公倍數,如此類推...數組
var dbList = []; //兩個數的最小公倍數 function gcd(a,b){ var minNum = Math.min(a,b),maxNum = Math.max(a,b),i=Math.floor(maxNum/minNum),vper=0; if(a ===0 || b===0){ return maxNum; } for(;i<=maxNum;i++){ vper = minNum * i; if(vper % maxNum === 0){ return vper; break; } } } //求一個數組的最下公倍數 function gcds(arr){ var onum = 0,i = 0,len = arr.length,midNum = 0; for(;i<len;i++){ onum = Math.floor(arr[i]);//去掉小數 midNum = gcd(midNum,onum); } return midNum; } console.log(gcds([2,5,8,21]))
感謝@朝聞道1991 指正。.net