How Do You Find Armstrong Number(s) in Python
One of the best things about Python is the ease with which you can perform complex calculations. For example, consider how we could use Python to verify or find Armstrong number patterns. The following Python tutorial will demonstrate how to leverage Python’s features to quickly and efficiently work with Armstrong numbers.
A Closer Look at Armstrong Numbers
You’re probably familiar with prime numbers. A prime number is a value greater than zero whose only factors are itself and one. An Armstrong number is somewhat similar. The sum of the cubes of the integer’s digits needs to be equal to the initial value. Essentially, an Armstrong number is an n-digit number equal to a total of the nth powers of the number’s digits.
Armstrong numbers seldom have any practical use. But they can show up in a test or interview in order to see how someone would handle a mathematical challenge. For example, a test might ask you to develop source code to discover or manipulate Armstrong numbers. And we can, in fact, do so quite easily with Python.
Armstrong Numbers and Python
Testing Armstrong numbers in Python is generally just a matter of feeding values into a formula. You can see how this process operates in the following python example.
originalValue = 153
numLen = len(str(originalValue))
inProgValue = originalValue
addValue = 0
while inProgValue != 0:
x = inProgValue % 10
addValue += x**numLen
inProgValue = inProgValue//10
if addValue == originalValue:
print(‘Armstrong’)
else:
print(‘Not Armstrong’)
We start off by declaring a given number to test. In this case we’ll check whether or not 153 is an Armstrong number. We need to begin by finding the number of digits in originalValue. This is best done in python by converting the value to a string and then running it through len. Next, we pass that value to a variable called inProgValue which will essentially serve as a placeholder to manipulate.
The declarations end by creating addValue as a repository for the sum of a calculation. In the next step, we go through each digit in our number, multiply it, and then add it to addValue. Finally, we check the total in addValue against the original number in originalValue. We can then print out whether or not the original value was an Armstrong number.
Bringing It All Together
The previous example is a good start. But we can build on the ideas presented there to vastly expand on the code’s scope. Consider the following example.
def isArmstrong(originalValue):
numLen = len(str(originalValue))
inProgValue = originalValue
addValue = 0
armstrongResult = 0
while inProgValue != 0:
x = inProgValue % 10
addValue += x**numLen
inProgValue = inProgValue//10
if addValue == originalValue:
armstrongResult = 1
else:
armstrongResult = 0
return armstrongResult
for x in range(0, 501):
if isArmstrong(x):
print(str(x)+” is Armstrong”)
else:
print(str(x)+” is not Armstrong”)
The fact that we’re creating a new function is the biggest change from the original code. We keep most of the original program logic, but it’s contained within a new isArmstrong function. We call the function by passing a single digit as originalValue. And the function ends by returning either a 1 or 0 to indicate whether the tested number is an Armstrong or not.
By putting the test within a function we now have the freedom to easily loop through it. For example, we could iterate through a python list full of specific numbers. Or we could use a for loop to iterate and test every individual digit within a set range of values. And we do exactly that within the context of this example. The for loop runs through a range of values while passing every iteration to isArmstrong. Each number is tested, and we print the result to screen within the if conditional.