Find Armstrong Numbers in an Interval in Python

I was working on a Python project where I needed to filter out all Armstrong numbers within a given range. Python doesn’t have a direct function for Armstrong numbers either.

So, I had to come up with a simple program to check each number in the interval and return only those that qualify as Armstrong numbers.

In this tutorial, I’ll show you how I solved this problem. I’ll cover multiple methods, from a basic loop to using functions and even a Pythonic one-liner.

Methods to Find Armstrong Numbers in an Interval

An Armstrong number (also called a narcissistic number) is a number that is equal to the sum of its digits raised to the power of the number of digits.

For example:

  • 153 → (1^3 + 5^3 + 3^3 = 153) (Armstrong)
  • 9474 → (9^4 + 4^4 + 7^4 + 4^4 = 9474) (Armstrong)
  • 123 → (1^3 + 2^3 + 3^3 = 36) (Not Armstrong)

This is the logic we’ll use in our Python program.

Method 1 – Use a For Loop

In my first attempt, I used a simple for loop in Python to check each number in the interval.

Here’s the code:

# Python program to find Armstrong numbers in an interval

# Define the interval
start = 100
end = 1000

print(f"Armstrong numbers between {start} and {end} are:")

for num in range(start, end + 1):
    # Convert number to string to get digits
    digits = str(num)
    power = len(digits)

    # Calculate sum of digits raised to the power
    total = sum(int(digit) ** power for digit in digits)

    if num == total:
        print(num)

Output:

Armstrong numbers between 100 and 1000 are:
153
370
371
407

You can see the output in the screenshot below.

armstrong number in python

If you run this program, you’ll see Armstrong numbers like 153, 370, 371, 407.

Method 2 – Use a Function

When I needed to reuse this logic in multiple places, I wrote a helper function in Python.

# Function to check Armstrong number
def is_armstrong(num):
    digits = str(num)
    power = len(digits)
    total = sum(int(digit) ** power for digit in digits)
    return num == total

# Find Armstrong numbers in interval
def find_armstrong(start, end):
    results = []
    for num in range(start, end + 1):
        if is_armstrong(num):
            results.append(num)
    return results

# Example usage
print(find_armstrong(1, 1000))

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407]

You can see the output in the screenshot below.

python program to check armstrong number using for loop

This approach is cleaner and makes the code reusable.

Method 3 – Use List Comprehension

If you want a more efficient way in Python, you can do this in just one line using list comprehension.

# Armstrong numbers in interval using list comprehension
start, end = 1, 1000

armstrong_numbers = [
    num for num in range(start, end + 1)
    if num == sum(int(digit) ** len(str(num)) for digit in str(num))
]

print(armstrong_numbers)

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407]

You can see the output in the screenshot below

what is armstrong number in python

This method is compact and efficient for quick scripts.

Method 4 – Use Recursion

As someone who often practices recursion to sharpen problem-solving skills, I also tried this with recursion.

# Recursive function to calculate Armstrong sum
def armstrong_sum(num, power):
    if num == 0:
        return 0
    digit = num % 10
    return (digit ** power) + armstrong_sum(num // 10, power)

# Check Armstrong numbers in interval
def find_armstrong_recursive(start, end):
    result = []
    for num in range(start, end + 1):
        power = len(str(num))
        if num == armstrong_sum(num, power):
            result.append(num)
    return result

print(find_armstrong_recursive(1, 1000))

This is a bit advanced, but it’s a neat exercise if you’re practicing recursion in Python.

Method 5 – Real-World Example (US ZIP Codes)

To make this more relatable for my readers in the USA, let’s apply this to a dataset of ZIP codes.

Imagine you want to check if any US ZIP codes between 10000 and 99999 are Armstrong numbers.

# Armstrong ZIP codes
zip_start, zip_end = 10000, 99999

armstrong_zips = [
    num for num in range(zip_start, zip_end + 1)
    if num == sum(int(digit) ** len(str(num)) for digit in str(num))
]

print("Armstrong ZIP codes:", armstrong_zips)

While rare, this demonstrates how you can apply the concept to real-world numeric ranges.

So, while Python doesn’t have a built-in function to find Armstrong numbers in an interval, we can easily create one using loops, functions, list comprehensions, or even recursion.

The loop method is simple and beginner-friendly.
The function method makes the code reusable.
The list comprehension method is concise and Pythonic.
And recursion is a great way to practice problem-solving.

I hope you found this tutorial helpful. If you have any questions or suggestions, feel free to drop them in the comments below.

Other Python tutorials you may also like:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.