0
0
PythonProgramBeginner · 2 min read

Python Program to Find All Armstrong Numbers in Range

You can find all Armstrong numbers in a range by looping through each number, calculating the sum of its digits raised to the power of the number of digits, and checking if this sum equals the number itself using code like for num in range(start, end+1): if num == sum(int(d)**len(str(num)) for d in str(num)): print(num).
📋

Examples

Inputstart=1, end=10
Output1 2 3 4 5 6 7 8 9
Inputstart=100, end=500
Output153 370 371 407
Inputstart=1500, end=1600
Output
🧠

How to Think About It

To find Armstrong numbers in a range, check each number one by one. For each number, count how many digits it has. Then, raise each digit to the power of that count and add them all together. If the total equals the original number, it is an Armstrong number.
📐

Algorithm

1
Get the start and end values of the range.
2
For each number in the range from start to end:
3
Count the number of digits in the number.
4
Calculate the sum of each digit raised to the power of the digit count.
5
If the sum equals the number, print or store it as an Armstrong number.
💻

Code

python
start = int(input('Enter start of range: '))
end = int(input('Enter end of range: '))

for num in range(start, end + 1):
    digits = str(num)
    power = len(digits)
    total = sum(int(d) ** power for d in digits)
    if num == total:
        print(num, end=' ')
Output
Enter start of range: 100 Enter end of range: 500 153 370 371 407
🔍

Dry Run

Let's trace the number 153 through the code to see why it is an Armstrong number.

1

Convert number to string

digits = '153'

2

Count digits

power = 3

3

Calculate sum of digits raised to power

1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153

4

Compare sum to original number

153 == 153 → True, so 153 is printed

NumberDigitsPowerSum of digits^powerIs Armstrong?
1531,5,33153Yes
1541,5,431 + 125 + 64 = 190No
💡

Why This Works

Step 1: Convert number to string

We convert the number to a string to easily access each digit.

Step 2: Count digits

The number of digits determines the power to which each digit is raised.

Step 3: Sum digits raised to power

Each digit is raised to the power of the digit count and summed to check if it equals the original number.

🔄

Alternative Approaches

Using a function to check Armstrong
python
def is_armstrong(num):
    digits = str(num)
    power = len(digits)
    return num == sum(int(d)**power for d in digits)

start, end = 1, 500
for n in range(start, end+1):
    if is_armstrong(n):
        print(n, end=' ')
This approach improves readability by separating the check into a function.
Using math.pow instead of ** operator
python
import math
start, end = 1, 500
for num in range(start, end+1):
    digits = str(num)
    power = len(digits)
    total = sum(int(d) ** power for d in digits)  # math.pow returns float, so ** is preferred
    if num == total:
        print(num, end=' ')
Using ** is simpler and avoids float conversion issues compared to math.pow.

Complexity: O(n * k) time, O(k) space

Time Complexity

The program checks each number in the range (n numbers). For each number, it processes each digit (k digits), so total time is O(n * k).

Space Complexity

Space is mainly used to store the string form of the number and temporary sums, which is O(k), where k is the number of digits.

Which Approach is Fastest?

Using the direct ** operator is faster and simpler than math.pow. Separating the check into a function improves readability but does not affect complexity.

ApproachTimeSpaceBest For
Inline check with ** operatorO(n * k)O(k)Simple scripts and beginners
Function-based checkO(n * k)O(k)Readability and reuse
Using math.powO(n * k)O(k)Avoided due to float overhead
💡
Convert the number to a string to easily iterate over each digit.
⚠️
Forgetting to raise each digit to the power of the total number of digits, which is key to identifying Armstrong numbers.