范围之间的Armstrong数字-JavaScript
如果以下方程式适用于该数字,则该数字称为阿姆斯壮数字-
xy..z = x^n + y^n+.....+ z^n
其中,n表示数字中的位数
例如-370是阿姆斯特朗数,因为-
3^3 + 7^3 + 0^3 = 27 + 343 + 0 = 370
我们需要编写一个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 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短