Finding Armstrong Numbers in an Interval

We will create two function for each method. One will be known as the ‘find_armstrong_number()’ which will be used to iterate through each element in the interval. Each element will then be passed to another function known as the ‘is_armstrong()’.

The ‘is_armstrong()’ function will be called by the first function and will take a parameter which will be the number from the interval. This function will actually calculate and check if the given number is an Armstrong number or not. If this function returns True for a number, that number will be added to the Armstrong numbers list, otherwise skipped.

Now that we know the basic structure of the code, let us see a few different ways to find all the Armstrong numbers in an Interval.

Using For Loop

This method involves using a for loop to iterate through each number in the interval and checking if it is an Armstrong number by calculating the sum of its digits each raised to the power of the number of digits.

Python
# function to find the armstrong number
def is_armstrong(number):
    
    digits = str(number)
    # calculate the length of the digit
    length = len(digits)
    # calculate the sum of digits each raised to the power of length
    sum_of_powers = sum(int(digit) ** length for digit in digits)
    # check if the sum of powers is equal to the original number
    return sum_of_powers == number

  
# function to iterate through the interval
def find_armstrong_numbers(start, end):
    armstrong_numbers = []
    # for loop to iterate through interval
    for num in range(start, end + 1):
        if is_armstrong(num):
            # add armsstrong number in the list
            armstrong_numbers.append(num)
    return armstrong_numbers

start = 100
end = 500

armstrong_numbers = find_armstrong_numbers(start, end)
print(f"Armstrong numbers between {start} and {end} are: {armstrong_numbers}")

Output:

Armstrong numbers between 100 and 500 are: [153, 370, 371, 407]

List Comprehension

List comprehensions provide a more Pythonic way to generate a list of Armstrong numbers within an interval. This method is concise and easy to read.

Python
# function to find the armstrong number
def is_armstrong(number):
    digits = str(number)
    # calculate the length of the digit
    length = len(digits)
    # calculate the sum of digits each raised to the power of length
    sum_of_powers = sum(int(digit) ** length for digit in digits)
    # check if the sum of powers is equal to the original number
    return sum_of_powers == number

  
# function to find armstrong number
def find_armstrong_numbers(start, end):
    # list con=mprehension 
    return [num for num in range(start, end + 1) if is_armstrong(num)]

  
start = 100
end = 500

armstrong_numbers = find_armstrong_numbers(start, end)
print(f"Armstrong numbers between {start} and {end} are: {armstrong_numbers}")

Output:

Armstrong numbers between 100 and 500 are: [153, 370, 371, 407]

Using Lambda and Filter Funtion

Using the filter() function along with lambda expressions can create an efficient and readable solution. This approach leverages Python’s capabilities in functional programming.

Python
# function to find the armstrong number
def is_armstrong(number):
    digits = str(number)
    # calculate the length of the digit
    length = len(digits)
    # calculate the sum of digits each raised to the power of length
    sum_of_powers = sum(int(digit) ** length for digit in digits)
    # check if the sum of powers is equal to the original number
    return sum_of_powers == number

  
# function to find armstrong number
def find_armstrong_numbers(start, end):
    # filter and lambda function
    return list(filter(lambda num: is_armstrong(num), range(start, end + 1)))


start = 100
end = 500

armstrong_numbers = find_armstrong_numbers(start, end)
print(f"Armstrong numbers between {start} and {end} are: {armstrong_numbers}")

Output:

Armstrong numbers between 100 and 500 are: [153, 370, 371, 407]




Python Program to Find Armstrong Number in an Interval

We are given an interval, that is a start and an end value. We have to find all the Armstrong numbers in the given range. In this article, we will discuss how to find the Armstrong Number in an interval in Python.

Similar Reads

Armstrong Number

A number is referred to as an Armstrong number if the sum of each digit raised to the power of the number of digits is equal to that number. It is also known as a narcissistic number. Some Armstrong numbers between 100 to 500 are:...

Finding Armstrong Numbers in an Interval

We will create two function for each method. One will be known as the ‘find_armstrong_number()’ which will be used to iterate through each element in the interval. Each element will then be passed to another function known as the ‘is_armstrong()’....