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 ofAlgorithm
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 )
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
Figure 1: Displaying results in the command window.
Example 2: Find the sum of all n-digit Armstrong numbers.
The Chinese translation ofAlgorithm
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 )
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
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Using Notepad++ to run a Python program requires the following steps: 1. Install the Python plug-in; 2. Create a Python file; 3. Set the run options; 4. Run the program.

PyCharm is a very popular Python integrated development environment (IDE). It provides a wealth of functions and tools to make Python development more efficient and convenient. This article will introduce you to the basic operation methods of PyCharm and provide specific code examples to help readers quickly get started and become proficient in operating the tool. 1. Download and install PyCharm First, we need to go to the PyCharm official website (https://www.jetbrains.com/pyc

PyCharm is a powerful Python integrated development environment that provides a wealth of functions and tools to help developers improve efficiency. Among them, PyInstaller is a commonly used tool that can package Python code into an executable file (EXE format) to facilitate running on machines without a Python environment. In this article, we will introduce how to use PyInstaller in PyCharm to package Python code into EXE format, and provide specific

Does PyCharm Community Edition support enough plugins? Need specific code examples As the Python language becomes more and more widely used in the field of software development, PyCharm, as a professional Python integrated development environment (IDE), is favored by developers. PyCharm is divided into two versions: professional version and community version. The community version is provided for free, but its plug-in support is limited compared to the professional version. So the question is, does PyCharm Community Edition support enough plug-ins? This article will use specific code examples to

Llama3 is here! Just now, Meta’s official website was updated and the official announced Llama 38 billion and 70 billion parameter versions. And it is an open source SOTA after its launch: Meta official data shows that the Llama38B and 70B versions surpass all opponents in their respective parameter scales. The 8B model outperforms Gemma7B and Mistral7BInstruct on many benchmarks such as MMLU, GPQA, and HumanEval. The 70B model has surpassed the popular closed-source fried chicken Claude3Sonnet, and has gone back and forth with Google's GeminiPro1.5. As soon as the Huggingface link came out, the open source community became excited again. The sharp-eyed blind students also discovered immediately

The Python program development process includes the following steps: Requirements analysis: clarify business needs and project goals. Design: Determine architecture and data structures, draw flowcharts or use design patterns. Writing code: Program in Python, following coding conventions and documentation comments. Testing: Writing unit and integration tests, conducting manual testing. Review and Refactor: Review code to find flaws and improve readability. Deploy: Deploy the code to the target environment. Maintenance: Fix bugs, improve functionality, and monitor updates.

What is GIL? GIL is the abbreviation of global interpreter lock, which is an important concept of python interpreter. The GIL ensures that the Python interpreter can only execute one thread at a time. This means that at any time, only one thread can run Python bytecode. Other threads must wait for the GIL to be available before continuing execution. How does GIL work? The GIL is a lock written in C and located in the Python interpreter. When a thread wants to execute Python bytecode, it must first obtain the GIL. If the GIL is already held by another thread, that thread must wait for the GIL to be available before continuing execution. What impact does the GIL have on Python programs? GIL for Python

Flask installation and configuration tutorial: A tool to easily build Python Web applications, specific code examples are required. Introduction: With the increasing popularity of Python, Web development has become one of the necessary skills for Python programmers. To carry out web development in Python, we need to choose a suitable web framework. Among the many Python Web frameworks, Flask is a simple, easy-to-use and flexible framework that is favored by developers. This article will introduce the installation of Flask framework,
