Write a program in Python to filter armstrong numbers in a given series

An Armstrong number (also called a narcissistic number) is a number that equals the sum of its digits raised to the power of the number of digits. For 3-digit numbers, each digit is cubed and summed. In this tutorial, we'll filter Armstrong numbers from a Pandas Series.

What is an Armstrong Number?

For a 3-digit number, if the sum of cubes of its digits equals the original number, it's an Armstrong number:

  • 153: 1³ + 5³ + 3³ = 1 + 125 + 27 = 153 ?

  • 371: 3³ + 7³ + 1³ = 27 + 343 + 1 = 371 ?

  • 323: 3³ + 2³ + 3³ = 27 + 8 + 27 = 62 ?

Algorithm Steps

To filter Armstrong numbers from a series, we follow these steps:

  • Create a Pandas Series with numbers

  • For each number, calculate the sum of cubes of its digits

  • Compare the result with the original number

  • Filter matching numbers using isin()

Complete Implementation

import pandas as pd

# Create a series with test numbers
data = pd.Series([153, 323, 371, 420, 500])
print("Original series:")
print(data)

# List to store Armstrong numbers
armstrong_list = []

# Check each number in the series
for val in data:
    armstrong_sum = 0
    temp = val
    
    # Calculate sum of cubes of digits
    while temp > 0:
        remainder = temp % 10
        armstrong_sum = armstrong_sum + (remainder ** 3)
        temp = temp // 10
    
    # If sum equals original number, it's an Armstrong number
    if armstrong_sum == val:
        armstrong_list.append(val)

# Filter Armstrong numbers from the series
print("\nArmstrong numbers:")
print(data[data.isin(armstrong_list)])
Original series:
0    153
1    323
2    371
3    420
4    500
dtype: int64

Armstrong numbers:
0    153
2    371
dtype: int64

How the Algorithm Works

The algorithm extracts each digit using modulo operation:

  • remainder = temp % 10 gets the last digit

  • armstrong_sum += remainder ** 3 adds the cube

  • temp = temp // 10 removes the last digit

  • Continue until temp > 0 becomes false

Alternative Function-Based Approach

import pandas as pd

def is_armstrong(num):
    """Check if a number is an Armstrong number"""
    digits = [int(d) for d in str(num)]
    power = len(digits)
    return sum(d ** power for d in digits) == num

# Create series and filter Armstrong numbers
data = pd.Series([153, 323, 371, 420, 500])
armstrong_mask = data.apply(is_armstrong)

print("Original series:")
print(data)
print("\nArmstrong numbers:")
print(data[armstrong_mask])
Original series:
0    153
1    323
2    371
3    420
4    500
dtype: int64

Armstrong numbers:
0    153
2    371
dtype: int64

Conclusion

Armstrong numbers can be filtered from a Pandas Series by calculating the sum of cubes of digits and comparing with the original number. Use isin() to filter the series or apply() with a custom function for a more concise approach.

Updated on: 2026-03-25T16:19:28+05:30

374 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements