5 Best Ways to Write a Python Program to Filter Armstrong Numbers in a Series

Rate this post

πŸ’‘ Problem Formulation: We aim to find Armstrong numbers within a given range or list. An Armstrong number is one that is equal to the sum of its own digits raised to the power of the number of digits. For instance, in the range 1 to 1000, an output might filter out 1, 153, 370, 371, 407 as Armstrong numbers.

Method 1: Using Traditional For Loops and If Statements

This method involves iterating over a range of numbers and checking if each number is an Armstrong number using a nested loop to sum the nth power of each digit. The function is simple to understand and is straightforward for someone who is new to Python.

Here’s an example:

def is_armstrong(number):
    total = 0
    num_str = str(number)
    length = len(num_str)
    for digit in num_str:
        total += int(digit) ** length
    return total == number

series = range(1, 1001)
armstrong_numbers = [num for num in series if is_armstrong(num)]

Output: [1, 153, 370, 371, 407]

The code defines a function is_armstrong() to check if a number is an Armstrong number. We then generate a list of Armstrong numbers from the series range using a list comprehension that employs the said function. Finally, we print out the results. It’s a clear and educative approach for beginners.

Method 2: Using Map and Filter Functions

The map and filter functions in Python provide a functional approach to filtering Armstrong numbers. This method is concise and leverages built-in functions for readability and improved performance.

Here’s an example:

series = range(1, 1001)
armstrong_numbers = list(filter(lambda num: num == sum(int(digit) ** len(str(num)) for digit in str(num)), series))

Output: [1, 153, 370, 371, 407]

This snippet uses a lambda function inside the filter() function to check for Armstrong numbers. The map() function isn’t explicitly used, but could be included for transformations. The filter() with a lambda function provides a powerful one-line solution to the problem.

Method 3: Using Generator Expressions

Generator expressions are like list comprehensions but are more memory efficient since they generate items on the fly. This method is suitable for large ranges or when memory usage is a concern.

Here’s an example:

def is_armstrong(number):
    digits = [int(d) for d in str(number)]
    power = len(digits)
    return number == sum(d ** power for d in digits)

armstrong_numbers = (num for num in range(1, 1001) if is_armstrong(num))

Output: [1, 153, 370, 371, 407]

In this approach, we use a generator expression to filter the Armstrong numbers. This is efficient as it processes elements one at a time, instead of creating a large list in memory. It’s a slight variation of the traditional loop approach but optimized for better performance.

Method 4: Using Recursion

Recursion can be used to break the problem into smaller instances. This method adds elegance to the solution and can be more intuitive for those who think recursively.

Here’s an example:

def sum_of_powers(number, power):
    if number == 0:
        return 0
        return (number % 10) ** power + sum_of_powers(number // 10, power)

def is_armstrong(number):
    return number == sum_of_powers(number, len(str(number)))

armstrong_numbers = [num for num in range(1, 1001) if is_armstrong(num)]

Output: [1, 153, 370, 371, 407]

The code defines a recursive function sum_of_powers() to calculate the sum of digits raised to a certain power. We then filter the Armstrong numbers in a range using the is_armstrong function that uses this recursive function. It demonstrates how recursion can simplify complex iterative tasks.

Bonus One-Liner Method 5: Using NumPy

If you’re working within the SciPy stack, NumPy can be an effective library to vectorize the operation of checking Armstrong numbers. This approach is highly optimized and concise, but requires an understanding of NumPy.

Here’s an example:

import numpy as np

def is_armstrong_vec(numbers):
    num_str = np.char.mod('%d', numbers)
    powers = np.char.str_len(num_str)
    return np.sum(np.power(np.chararray.tolist((np.char.replace(num_str, '0', ' ').split())), powers[:, None]), axis=1) == numbers

series = np.arange(1, 1001)
armstrong_numbers = series[is_armstrong_vec(series)]

Output: [ 1 153 370 371 407]

This snippet demonstrates how to vectorize the operation of checking for Armstrong numbers using NumPy’s vectorized string operations and broadcasting. Although powerful, this approach demands familiarity with NumPy’s functions and may be less readable for those not well-versed in vectorized operations.


  • Method 1: For Loops and If Statements. Easy to understand. May not be the most efficient for large series.
  • Method 2: Map and Filter Functions. Elegant and functional. Potentially less performant due to lambda overhead.
  • Method 3: Generator Expressions. Memory efficient. Less intuitive for those unfamiliar with generators.
  • Method 4: Recursion. Simplifies the code. Can be less efficient due to call stack limitations and overhead.
  • Method 5: NumPy. Highly optimized for performance. Requires additional knowledge and less readable for non-NumPy users.