Armstrong number in python

An Armstrong number, also known as a narcissistic number, is a special type of number that possesses a unique mathematical property. The property states that the number is equal to the sum of each of its digits raised to the power of the number of digits in the number.

abcd...(n digits) = a^n + b^n + c^n + ... 

For example, an Armstrong number of order 3 is a number in which the sum of cubes (power of 3) of its digits is equal to the number itself. Let’s take the example of the number 153, which is an Armstrong number of order 3, because 1^3 + 5^3 + 3^3 = 153. Understanding this concept can be both interesting and useful in various fields of mathematics and computer science.

# Example
407 = 4*4*4 + 0*0*0 + 7*7*7
407 = 64 + 0 + 343
So, 407 is an Armstrong number of order 3.

Python program to find given number is Armstrong number or not (nth order)

# armstrongnum.py
# Function to find given number is armstrong or not.
def isArmstrong(num):
    order = len(str(num))
    sum = 0
    curr = num
    while(curr):
        digit = curr % 10
        curr = curr / 10
        sum += digit ** order
    if sum == num:
        return True
    return False

# checks if 153 is armstrong number or not.
print(isArmstrong(153))
# checks if 256 is armstrong number or not.
print(isArmstrong(256))
# Output
$python armstrongnum.py
True
False

The provided program is a python script that checks if a given number is an Armstrong number or not. The function is defined as “isArmstrong(num)” which takes an argument ‘num’ and returns a boolean value indicating whether the number is an Armstrong number or not.

The function starts by initialising a variable “order” and assigns it the number of digits in the given number. This is done by converting the number to a string and finding its length.

Then it initialises a variable “sum” and assigns it the value of 0. This variable will be used to store the sum of the each digit raised to the power of the number of digits in the number.

It then uses a while loop to iterate through each digit of the number. In each iteration, it uses the modulo operator to get the last digit of the number, assigns it to a variable “digit” and using floor division operator it removes the last digit from the number. Then it raises the digit to the power of the number of digits and adds the result to the “sum” variable.

After the loop, it checks if the value of “sum” is equal to the number passed as an argument, if it is, it returns True, indicating that the number is an Armstrong number. If not, it returns False.

The program then calls the isArmstrong function with an argument of 153, in this case, it will return True as 153 is an Armstrong number of order 3 (1^3 + 5^3 + 3^3 = 153).

This program uses a simple approach to check if a number is an Armstrong number or not, by iterating through each digit of the number, raising it to the power of the number of digits and summing it up. It then compares this sum to the original number passed as an argument. If the sum is equal to the original number, it returns True indicating that the number is an Armstrong number, otherwise, it returns False. This program is efficient and easy to understand, making it a useful tool for anyone interested in working with Armstrong numbers.

Now, we use the same above function and find all armstrong numbers below 1000 using below python program.

Python program to list all armstrong numbers less than given number

# listarmstrongnums.py
# Function to find given number is armstrong or not.
def isArmstrong(num):
    order = len(str(num))
    sum = 0
    curr = num
    while(curr):
        digit = curr % 10
        curr = curr / 10
        sum += digit ** order
    if sum == num:
        return True
    return False

# prints all armstrongs less than given numbers.
def listArmstrongs(num):
    for val in range(1, num):
        if isArmstrong(val):
            print(val)

# lists all armstrong numbers below 1000.
listArmstrongs(1000)

Above code lists all armstrong numbers with different ordres 1, 2 and 3rd order armstrong numbers. Below is the output of the program.

# Output
$ python listarmstrongnums.py
1
2
3
4
5
6
7
8
9
153
370
371
407

In summary, the above program includes two functions that work together to generate a list of all Armstrong numbers less than a given input, in this case, 1000. By combining these two functions, we are able to easily find and understand Armstrong numbers and their properties. We hope this information has been helpful in providing a comprehensive understanding of Armstrong numbers and how to work with them using Python. Keep exploring and learning!