范围之间的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 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短