Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to Get Sum of N Armstrong Numbers
An Armstrong number (also called a narcissistic number) is a number where the sum of its digits raised to the power of the total number of digits equals the number itself. For example, 153 is a 3-digit Armstrong number because 1³ + 5³ + 3³ = 1 + 125 + 27 = 153.
This tutorial shows how to find and sum all Armstrong numbers of a given digit length using Python.
What is an Armstrong Number?
Let's first understand the concept with a simple function to check if a number is an Armstrong number ?
def is_armstrong(num):
# Convert to string to count digits
digits = str(num)
num_digits = len(digits)
# Calculate sum of digits raised to power of total digits
total = sum(int(digit) ** num_digits for digit in digits)
return total == num
# Test with some examples
test_numbers = [153, 370, 371, 407, 1634]
for num in test_numbers:
if is_armstrong(num):
print(f"{num} is an Armstrong number")
153 is an Armstrong number 370 is an Armstrong number 371 is an Armstrong number 407 is an Armstrong number 1634 is an Armstrong number
Method 1: Sum of 3-Digit Armstrong Numbers
Let's find all 3-digit Armstrong numbers and calculate their sum ?
def find_armstrong_numbers(num_digits):
armstrong_numbers = []
# Range for n-digit numbers
start = 10 ** (num_digits - 1)
end = 10 ** num_digits
for num in range(start, end):
# Calculate sum of digits raised to power
temp = num
digit_sum = 0
while temp > 0:
digit = temp % 10
digit_sum += digit ** num_digits
temp //= 10
# Check if it's an Armstrong number
if digit_sum == num:
armstrong_numbers.append(num)
return armstrong_numbers
# Find 3-digit Armstrong numbers
three_digit_armstrong = find_armstrong_numbers(3)
print("3-digit Armstrong numbers:", three_digit_armstrong)
# Calculate their sum
total_sum = sum(three_digit_armstrong)
print(f"Sum of all 3-digit Armstrong numbers: {total_sum}")
3-digit Armstrong numbers: [153, 370, 371, 407] Sum of all 3-digit Armstrong numbers: 1301
Method 2: Sum of N-Digit Armstrong Numbers
This approach allows you to find Armstrong numbers for any digit length ?
def get_armstrong_sum(num_digits):
armstrong_numbers = []
# Define range for n-digit numbers
if num_digits == 1:
start, end = 1, 10
else:
start = 10 ** (num_digits - 1)
end = 10 ** num_digits
print(f"Searching {num_digits}-digit numbers from {start} to {end-1}")
for num in range(start, end):
# Calculate sum using string manipulation (more efficient)
digits = [int(d) for d in str(num)]
digit_sum = sum(digit ** num_digits for digit in digits)
if digit_sum == num:
armstrong_numbers.append(num)
return armstrong_numbers, sum(armstrong_numbers)
# Test with different digit lengths
for digits in [3, 4, 5]:
numbers, total = get_armstrong_sum(digits)
print(f"\n{digits}-digit Armstrong numbers: {numbers}")
print(f"Sum: {total}")
Searching 3-digit numbers from 100 to 999 3-digit Armstrong numbers: [153, 370, 371, 407] Sum: 1301 Searching 4-digit numbers from 1000 to 9999 4-digit Armstrong numbers: [1634, 8208, 9474] Sum: 19316 Searching 5-digit numbers from 10000 to 99999 5-digit Armstrong numbers: [54748, 92727, 93084] Sum: 240559
Comparison of Methods
| Method | Approach | Best For |
|---|---|---|
| Mathematical | Extract digits using modulo | Understanding the algorithm |
| String-based | Convert to string and iterate | Cleaner, more readable code |
Optimized Version
Here's a more efficient version that combines both approaches ?
def armstrong_sum_optimized(max_digits=6):
results = {}
for num_digits in range(1, max_digits + 1):
armstrong_numbers = []
# Define search range
start = 1 if num_digits == 1 else 10 ** (num_digits - 1)
end = 10 ** num_digits
for num in range(start, end):
# Quick check using string conversion
if sum(int(d) ** num_digits for d in str(num)) == num:
armstrong_numbers.append(num)
results[num_digits] = {
'numbers': armstrong_numbers,
'sum': sum(armstrong_numbers),
'count': len(armstrong_numbers)
}
return results
# Get results for digits 1-5
results = armstrong_sum_optimized(5)
for digits, data in results.items():
if data['numbers']: # Only show if Armstrong numbers exist
print(f"{digits}-digit: {data['numbers']} ? Sum: {data['sum']}")
1-digit: [1, 2, 3, 4, 5, 6, 7, 8, 9] ? Sum: 45 3-digit: [153, 370, 371, 407] ? Sum: 1301 4-digit: [1634, 8208, 9474] ? Sum: 19316 5-digit: [54748, 92727, 93084] ? Sum: 240559
Conclusion
Armstrong numbers are found by checking if the sum of digits raised to the power of digit count equals the original number. Use the mathematical approach for learning or the string-based method for cleaner code. The optimized version efficiently handles multiple digit lengths at once.
