【JavaScript基礎アルゴリズム総括編(二)】最大公約数と最小公倍数*/*検索1~1000以内の整数
5436 ワード
var res = a*b;
while(b!=0){
var m = a%b;
a = b;
b = m;
};
console.log(" :",a);
console.log(" :",res/a);
/**
*@OUTPUT
:4
:24
*/
for(var i = 1;i <= 1000;i++){
var arr1 = [];
var sum = 0;
for(var j = 1;j < i; j++){
switch(i % j){
case 0:
arr1.push(j);
};
};
for(var k = 0;k < arr1.length;k++){
sum += arr1[k];
};
if(sum == i){
console.log(i+' ',arr1);
};
};
/**
*@OUTPUT
6 [ 1, 2, 3 ]
28 [ 1, 2, 4, 7, 14 ]
496 [ 1, 2, 4, 8, 16, 31, 62, 124, 248 ]
*/