Home > Backend Development > Python Tutorial > Python program to get the sum of N Armstrong numbers

Python program to get the sum of N Armstrong numbers

WBOY
Release: 2023-09-12 10:41:02
forward
611 people have browsed it

Python program to get the sum of N Armstrong numbers

A number is said to be Armstrong if each of its digits is raised to the power of the total number of digits and then the subparts are added together and the result is equal to the number. number. In this Python example, a method for finding the sum of n-digit Armstrong numbers is given using two different examples. In Example 1, a method for calculating the sum of all 3-digit Armstrong numbers is given. In Example 2, the user can decide the number of bits at runtime. The program tests using 4 to 6 digit numbers.

Example 1 - Find the sum of all 3-digit Armstrong numbers.

The Chinese translation of

Algorithm

is:

Algorithm

Step 1 - Get a list of all three digits. Call this list listofallNums.

Step 2 - Create a function that returns the sum of all numbers raised to the 3rd power of a number if the calculated sum is equal to the number itself, otherwise it returns -1.

Step 3 − For all numbers in listofallNums, call the above function, and if the value is not -1, add it to the list named listofArmStrNums.

Step 4 - Verify that all numbers in the ArmStrNums list are 3-digit armStrong numbers. Now add all these 3 digit ArmStrong numbers together.

Step 5 - Run the program and check the results.

Python file contains this content

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 )
Copy after login

View results - Example 1

To see the results, run the Python file in a command line 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
Copy after login

Figure 1: Displaying results in the command window.

Example 2: Find the sum of all n-digit Armstrong numbers.

The Chinese translation of

Algorithm

is:

Algorithm

Step 1 - Enter the value N of a number and get a list of all N digits. Call this list listofallNums.

Step 2 - Create a function that returns the sum of all numbers raised to the power N if the calculated sum is equal to the number itself, otherwise it returns -1.

Step 3 − For all numbers in listofallNums, call the above function, and if the value is not -1, add it to the list named listofArmStrNums.

Step 4 − Verify whether all the numbers in the listofArmStrNums are N-digit Armstrong numbers. Now add all these N-digit Armstrong numbers.

Step 5 - Run the program and check if there are 4 and 5 digit numbers in the result.

Python file contains this content

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 )
Copy after login

View results - Example 2

Open the cmd window and run the python file to see the results.

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
Copy after login

Figure 2: Showing the sum and n-digit Armstrong numbers.

In this Python article, using two different examples, the method of finding the sum of n-digit Armstrong numbers is given. In Example 1, a method for calculating the sum of all 3-digit Armstrong numbers is given. In Example 2, the user can decide the number of digits at runtime. If the user enters 4, then all 4-digit Armstrong numbers and their sum are given.

The above is the detailed content of Python program to get the sum of N Armstrong numbers. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template