Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Armstrong number within a range in JavaScript
Armstrong Numbers: A positive integer is called an Armstrong number (of order n) if ?
abcd... = a^n + b^n + c^n + d^n + ...
where n is the number of digits. For example, 153 is an Armstrong number because 1³ + 5³ + 3³ = 1 + 125 + 27 = 153.
We are required to write a JavaScript function that takes in an array of exactly two numbers specifying a range. The function should return an array of all the Armstrong numbers that falls in that range (including the start and end numbers if they are Armstrong).
We will first separately write a function to detect Armstrong numbers and then iterate through the range to fill the array with desired numbers.
Understanding Armstrong Numbers
An Armstrong number is equal to the sum of its digits raised to the power of the number of digits:
- 1-digit: 1, 2, 3, 4, 5, 6, 7, 8, 9 (all single digits)
- 3-digit: 153 = 1³ + 5³ + 3³ = 1 + 125 + 27 = 153
- 4-digit: 1634 = 1? + 6? + 3? + 4? = 1 + 1296 + 81 + 256 = 1634
Step 1: Function to Check Armstrong Number
const isArmstrong = (num) => {
const numberOfDigits = ('' + num).length;
let sum = 0;
let temp = num;
while (temp > 0) {
let remainder = temp % 10;
sum += remainder ** numberOfDigits;
temp = parseInt(temp / 10);
}
return sum === num;
};
// Test the function
console.log(isArmstrong(153)); // true
console.log(isArmstrong(154)); // false
console.log(isArmstrong(9)); // true
true false true
Step 2: Finding Armstrong Numbers in Range
const findAllArmstrong = ([start, end]) => {
const res = [];
for(let i = start; i <= end; i++){
if(isArmstrong(i)){
res.push(i);
}
}
return res;
};
// Test with different ranges
const range = [11, 1111];
console.log("Armstrong numbers between 11 and 1111:", findAllArmstrong(range));
const smallRange = [1, 20];
console.log("Armstrong numbers between 1 and 20:", findAllArmstrong(smallRange));
Armstrong numbers between 11 and 1111: [ 153, 370, 371, 407 ] Armstrong numbers between 1 and 20: [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
Complete Example
const range = [11, 1111];
const isArmstrong = (num) => {
const numberOfDigits = ('' + num).length;
let sum = 0;
let temp = num;
while (temp > 0) {
let remainder = temp % 10;
sum += remainder ** numberOfDigits;
temp = parseInt(temp / 10);
}
return sum === num;
};
const findAllArmstrong = ([start, end]) => {
const res = [];
for(let i = start; i <= end; i++){
if(isArmstrong(i)){
res.push(i);
}
}
return res;
};
console.log(findAllArmstrong(range));
[ 153, 370, 371, 407 ]
How It Works
The algorithm works in two steps:
- isArmstrong function: Extracts each digit, raises it to the power of total digits, and sums them up
- findAllArmstrong function: Iterates through the range and collects all Armstrong numbers using the helper function
Conclusion
Finding Armstrong numbers in a range involves checking each number by summing its digits raised to the power of digit count. This approach efficiently identifies all Armstrong numbers within any given range.
