在JavaScript中查找给定范围内的阿姆斯壮数字
如果以下方程式适用于该数字,则该数字称为阿姆斯特朗数字:xy...z=xx+yy+...+zz,其中n表示数字中的位数。
例如:
153是Armstrong的数字,因为-
11 +55 +33 = 1 + 125 + 27 =153
我们需要编写一个JavaScript函数,该函数接受两个数字(一个范围),并返回它们之间的所有数字(即阿姆斯特朗(包括阿姆斯特朗),包括阿姆斯特朗)
示例
为此的代码将是-
const isArmstrong = number => {
let num = number;
const len = String(num).split("").length;
let res = 0;
while(num){
const last = num % 10;
res += Math.pow(last, len);
num = Math.floor(num / 10);
};
return res === number;
};
const armstrongBetween = (lower, upper) => {
const res = [];
for(let i = lower; i <= upper; i++){
if(isArmstrong(i)){
res.push(i);
};
};
return res;
};
console.log(armstrongBetween(1, 400));输出结果
控制台中的输出-
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371 ]
热门推荐
10 写给男生简短的祝福语
11 车行销售祝福语简短
12 浪漫的未来祝福语简短
13 婚礼结束聚餐祝福语简短
14 中秋祝福语送朋友简短
15 周一情人祝福语简短
16 50多岁生日简短祝福语
17 对生活祝福语简短精辟
18 简短大气的狗年祝福语