Armstrong numbers and a Python program to find it

Author Topic: Armstrong numbers and a Python program to find it  (Read 1551 times)

Offline momin.ce

  • Newbie
  • *
  • Posts: 28
  • Today is a gift. Use it properly.
    • View Profile
    • Khondhaker Al Momin
Armstrong numbers and a Python program to find it
« on: February 20, 2020, 07:52:02 PM »
What is an Armstrong number?

An Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits.

for example:
1. 153 is an Armstrong number because it is a three-digit number and
 (1^3) + (5^3) + (3^3)= 153.

2. Again 407 is an Armstrong number because it is a three-digit number and
(4^3) + (0^3) + (7^3) = 407.

3. 1634 is also an Armstrong number. It is a four-digit number and
(1^4)+(6^4)+(3^4)+(4^4)=1634

Hopefully, now you get what is an Armstrong number.
Now it is very tough to check large numbers whether it is an Armstrong number or not. But it 2020 and we have programming languages. I have written a Python code to determine all the Armstong numbers from 0 to 15,0000000
and these numbers are:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725, 4210818, 9800817, 9926315, 24678050, 24678051, 88593477, 146511208.

By the way, you can set the upper limit as much you want but if you set a very large number as upper limit, it might take a longer time to determine.

You can copy the codes and paste in any text file and save as .py

upper_limit=int(input("Input the upper limit "))
for i in range (upper_limit):
    number =int(i)
    summation = 0
    temp = number
    length = len(str(number))
    while number != 0:
        number_mod = number % 10
        summation = summation + number_mod ** length
        number = number // 10
    if summation == temp:
        print(temp,end=", ")


Lecturer,
Department of Civil Engineering,
Daffodil International Unversity.