
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get Sum of N Armstrong Numbers in Python
A number is said to be an Armstrong number if the digits are taken individually and raised to the power of the total digits and then these subparts are added together and they give the number itself. Here, in this Python example, using two different examples, the method of finding the total sum of n-digit Armstrong numbers is given. In example 1, the method to calculate the sum of all the 3 digit Armstrong numbers is given. In example 2, the number of digits can be decided by the user at runtime. This program is tested using digit values 4 to 6.
Example 1 - Find the sum of all the 3 digit Armstrong numbers.
Algorithm
Step 1 ? Get the list of all 3 digits numbers. Call the list the listofallNums.
Step 2 ? Make a function that will return the sum of all the digits of a number raised to the power 3, if the sum calculated will be equal to the number itself, otherwise it will return -1.
Step 3 ? For all the numbers in listofallNums call the above function and if the value is not -1, add it to the list called listofArmStrNums.
Step 4 ? Verify that all numbers in the listofArmStrNums are the armStrong numbers of 3 digits. Now add all these 3 digits ArmStrong numbers together.
Step 5 ? Run the program and then check the result.
The Python File Contains this
numOfDigits=3 listofallNums=[] listofArmStrNums=[] listofsumparts=[] def isArmStr(num, powof): sum = 0 TNum = num while TNum > 0: digitt = TNum % 10 sum += digitt ** powof TNum = TNum//10 if sum==num: print('This is an Armstrong number', num) return sum else: return -1 lowerNum=10**(numOfDigits-1) highNum=10**(numOfDigits) for item in range(lowerNum, highNum): listofallNums.append(item) lastelem=len(listofallNums)-1 print("listofallNums contains numbers from ",listofallNums[0], " to ", listofallNums[lastelem]) for itemn in listofallNums: if(isArmStr(itemn, numOfDigits) != -1): listofArmStrNums.append(itemn) print("List of ArmStrong Numbers: ", listofArmStrNums) for elem in listofArmStrNums: listofsumparts=[] summ=0 while elem > 0: digittt = elem % 10 listofsumparts.append(digittt ** numOfDigits) elem = elem//10 print(listofsumparts) total=0 print("adding together: ") for ele in range(0,len(listofsumparts)): total = total + listofsumparts[ele] print(total) SumofallArmStrongnumbers=0 for element in listofArmStrNums: SumofallArmStrongnumbers = SumofallArmStrongnumbers + element print("Sum of all 3 digit ArmStrong numbers is ", SumofallArmStrongnumbers )
Viewing The Result - Example 1
For seeing the result run the Python file in the cmd window.
listofallNums contains numbers from 100 to 999 This is an Armstrong number 153 This is an Armstrong number 370 This is an Armstrong number 371 This is an Armstrong number 407 List of ArmStrong Numbers: [153, 370, 371, 407] [27, 125, 1] adding together: 153 [0, 343, 27] adding together: 370 [1, 343, 27] adding together: 371 [343, 0, 64] adding together: 407 Sum of all 3 digit ArmStrong numbers is 1301
Fig 1: Showing the result in the command window.
Example 2: Find the sum of of all the n digit Armstrong numbers.
Algorithm
Step 1 ? Enter the value of the digit as N and get the list of all N digits numbers. Call the list the listofallNums.
Step 2 ? Make a function that will return the sum of all the digits of a number raised to the power N, if the sum calculated will be equal to the number itself, otherwise it will return -1.
Step 3 ? For all the numbers in listofallNums call the above function and if the value is not -1, add it to the list called listofArmStrNums.
Step 4 ? Verify that all numbers in the listofArmStrNums are the armStrong numbers of N digits. Now add all these N digits ArmStrong numbers together.
Step 5 ? Run the program and then check the result for 4 digits and 5-digit numbers.
The Python File Contains this
numOfDigits = 5 listofallNums=[] listofArmStrNums=[] listofsumparts=[] def isArmStr(num, powof): sum = 0 TNum = num while TNum > 0: digitt = TNum % 10 sum += digitt ** powof TNum = TNum//10 if sum==num: print('This is an Armstrong number', num) return sum else: return -1 lowerNum=10**(numOfDigits-1) highNum=10**(numOfDigits) for item in range(lowerNum, highNum): listofallNums.append(item) lastelem=len(listofallNums)-1 print("listofallNums contains numbers from ",listofallNums[0], " to ", listofallNums[lastelem]) for itemn in listofallNums: if(isArmStr(itemn, numOfDigits) != -1): listofArmStrNums.append(itemn) print("List of ArmStrong Numbers: ", listofArmStrNums) for elem in listofArmStrNums: listofsumparts=[] summ=0 while elem > 0: digittt = elem % 10 listofsumparts.append(digittt ** numOfDigits) elem = elem//10 print("list of sum subparts: ", listofsumparts) total=0 print("adding together: ") for ele in range(0,len(listofsumparts)): total = total + listofsumparts[ele] print(total) SumofallArmStrongnumbers=0 for element in listofArmStrNums: SumofallArmStrongnumbers = SumofallArmStrongnumbers + element print("Sum of all ", numOfDigits, " digit ArmStrong numbers is ", SumofallArmStrongnumbers )
Viewing The Result - Example 2
Open the cmd window and run the python file to see the result.
listofallNums contains numbers from 10000 to 99999 This is an Armstrong number 54748 This is an Armstrong number 92727 This is an Armstrong number 93084 List of ArmStrong Numbers: [54748, 92727, 93084] list of sum subparts: [32768, 1024, 16807, 1024, 3125] adding together: 54748 list of sum subparts: [16807, 32, 16807, 32, 59049] adding together: 92727 list of sum subparts: [1024, 32768, 0, 243, 59049] adding together: 93084 Sum of all 5 digit ArmStrong numbers is 240559
Fig 2: Showing the sums and the n digits Armstrong numbers.
In this Python article, using two different examples, the method of finding the total sum of n-digit Armstrong numbers is given. In example 1, the method to calculate the sum of all the 3 digit Armstrong numbers is given. In example 2, the number of digits can be decided by the user at runtime. If the user enters 4 it gives all the 4 digits of Armstrong numbers and their sum.