xy...z =xx +yy+...+zz 방정식이 해당 숫자에 해당하는 경우 숫자를 암스트롱 숫자라고 합니다. 여기서 n은 숫자의 자릿수를 나타냅니다.
예:
153은 암스트롱 수입니다. -
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 ]