What is an Armstrong Number?
An Armstrong number (or narcissistic number) in a given number base is a number that equals the sum of its own digits, each raised to the power of the number of digits. These numbers are a fascinating concept in number theory and a common starting point for beginners in programming. For example, in base 10:
Example:
153 = 1³ + 5³ + 3³ = 153
Key Properties:
- An Armstrong number can exist in any number base.
- In base 10, single-digit numbers (0 and 1) are considered Armstrong numbers.
Armstrong Number Examples
Here are some examples of Armstrong numbers in the decimal system:
- 3-Digit Numbers:
- 153: 1³ + 5³ + 3³ = 153
- 370: 3³ + 7³ + 0³ = 370
- 371: 3³ + 7³ + 1³ = 371
- 407: 4³ + 0³ + 7³ = 407
- 4-Digit Numbers:
- 1634: 1⁴ + 6⁴ + 3⁴ + 4⁴ = 1634
- 8208: 8⁴ + 2⁴ + 0⁴ + 8⁴ = 8208
- 9474: 9⁴ + 4⁴ + 7⁴ + 4⁴ = 9474
Armstrong Numbers in Base 3
Even in other bases, Armstrong numbers exist. For example: 122 (base 3) = 1³ + 2³ + 2³ = 17 (decimal).
In this course, you will learn the fundamentals of Python: from basic syntax to mastering data structures, loops, and functions. You will also explore OOP concepts and objects to build robust programs.
How to Check for Armstrong Numbers?
To determine whether a number is an Armstrong number, follow this algorithm:
Algorithm:
- Input: Take the number to be checked.
- Find Digits: Determine the number of digits (“n”).
- Process Each Digit:
- Extract each digit using modulus (num % 10).
- Raise the digit to the power of “n”.
- Add the result to a running total.
- Divide the number by 10 to move to the next digit.
- Compare: If the total equals the original number, it’s an Armstrong number.
Example Algorithm in Python:
num = int(input("Enter a number: "))
original = num
sum = 0
n = len(str(num))
while num > 0:
digit = num % 10
sum += digit ** n
num //= 10
if sum == original:
print(f"{original} is an Armstrong Number!")
else:
print(f"{original} is not an Armstrong Number.")
Output Example:
Enter a number: 153
153 is an Armstrong Number!
Python Programs for Armstrong Numbers
Check a Specific Number:
num = int(input("Enter a number: "))
original = num
sum = 0
n = len(str(num))
while num > 0:
digit = num % 10
sum += digit ** n
num //= 10
if sum == original:
print(f"{original} is an Armstrong Number.")
else:
print(f"{original} is not an Armstrong Number.")
Output Example:
Enter a number: 123
123 is not an Armstrong Number.
Find Armstrong Numbers in a Range:
t1 = int(input("Enter the minimum value: "))
t2 = int(input("Enter the maximum value: "))
print("Armstrong numbers in the range:")
for num in range(t1, t2 + 1):
original = num
sum = 0
n = len(str(num))
while num > 0:
digit = num % 10
sum += digit ** n
num //= 10
if sum == original:
print(original)
Output Example:
Enter the minimum value: 100
Enter the maximum value: 500
Armstrong numbers in the range: 153, 370, 371, 407
Print the First “N” Armstrong Numbers:
N = int(input("Enter the number of Armstrong numbers to find: "))
count = 0
num = 1
print("The first", N, "Armstrong numbers are:")
while count < N:
original = num
sum = 0
n = len(str(num))
while num > 0:
digit = num % 10
sum += digit ** n
num //= 10
if sum == original:
print(original)
count += 1
num += 1
Output Example:
Enter the number of Armstrong numbers to find: 5
The first 5 Armstrong numbers are: 1, 153, 370, 371, 407
Conclusion
In this blog, we explored the concept of Armstrong numbers, their properties, examples, and Python implementations. Armstrong numbers may not have real-world applications but are a fun and educational topic for programming practice. For more tutorials on Python and machine learning, stay tuned!
Also Read: