Fundamentals 5 min read

Identify Armstrong (Narcissistic) Numbers Using Simple Python Scripts

This article explains what Armstrong (narcissistic) numbers are, demonstrates how to determine them for three‑digit, n‑digit, and range‑based cases, and provides clear Python code examples with sample outputs to help readers grasp the concept and implement the checks.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Identify Armstrong (Narcissistic) Numbers Using Simple Python Scripts

An Armstrong number (also called a narcissistic number) is a positive integer that equals the sum of its own digits each raised to the power of the number of digits. For example, 153 = 1³ + 5³ + 3³, so 153 is a three‑digit Armstrong number.

1. Check Armstrong number (3‑digit)

# 检查该数字是否为阿姆斯壮数字的Python程序
# 接受用户的输入
num = int(input("输入一个数字: "))
# 初始化sum
sum = 0
# 求出每个数字的立方和
temp = num
while temp > 0:
   digit = temp % 10
   sum += digit ** 3
   temp //= 10
# 显示结果
if num == sum:
   print(num, "是阿姆斯特朗数")
else:
   print(num, "不是阿姆斯特朗数")

Sample output for input 153:

Sample output for a non‑Armstrong number:

2. Check n‑digit Armstrong number

# 将num变量更改为string
# 并计算出长度(位数)
order = len(str(num))
# 初始化 sum
sum = 0
# 求出每个数字的立方和
temp = num
while temp > 0:
    digit = temp % 10
    sum += digit ** order
    temp //= 10
# 显示结果
if num == sum:
    print(num, "是阿姆斯特朗数")
else:
    print(num, "不是阿姆斯特朗数")

Sample output for input 1634 (a 4‑digit Armstrong number):

3. Find Armstrong numbers within an integer range

# Python程序在整数中查找阿姆斯特朗数
lower = 100
upper = 2000

for num in range(lower, upper + 1):
    # order 个数
    order = len(str(num))
    # 初始化 sum
    sum = 0
    temp = num
    while temp > 0:
        digit = temp % 10
        sum += digit ** order
        temp //= 10
    if num == sum:
        print(num)

Sample output (Armstrong numbers between 100 and 2000):

Summary

This article, based on basic Python, introduces the concept of Armstrong numbers, shows how to determine them for three‑digit, arbitrary‑digit, and range‑based cases, and provides clear code examples with output screenshots to help readers understand and apply the algorithm.

Using Python makes the logic easy to follow, and readers can modify the code (e.g., change the input number or range) to experiment further.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

algorithmPythonnumber theoryArmstrong numbersnarcissistic numbers
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.