search
HomeBackend DevelopmentPython TutorialPython program to get the sum of N Armstrong numbers

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 )

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 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 )

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!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
PyCharm高级教程:利用PyInstaller将代码打包为EXE格式PyCharm高级教程:利用PyInstaller将代码打包为EXE格式Feb 20, 2024 am 09:34 AM

PyCharm是一款功能强大的Python集成开发环境,提供了丰富的功能和工具来帮助开发者提高效率。其中,PyInstaller是一个常用的工具,可以将Python代码打包为可执行文件(EXE格式),方便在没有Python环境的机器上运行。在本篇文章中,我们将介绍如何在PyCharm中使用PyInstaller将Python代码打包为EXE格式,并提供具体的

PyCharm社区版支持的插件足够吗?PyCharm社区版支持的插件足够吗?Feb 20, 2024 pm 04:42 PM

PyCharm社区版支持的插件足够吗?需要具体代码示例随着Python语言在软件开发领域的应用越来越广泛,PyCharm作为一款专业的Python集成开发环境(IDE),备受开发者青睐。PyCharm分为专业版和社区版两个版本,其中社区版是免费提供的,但其插件支持相对专业版有所限制。那么问题来了,PyCharm社区版支持的插件足够吗?本文将通过具体的代码示例

Python元编程:赋予你编程超能力的工具Python元编程:赋予你编程超能力的工具Feb 19, 2024 pm 04:45 PM

python元编程是一种强大的技术,它允许你对Python语言本身进行操作,赋予你编程超能力。元编程可以通过使用元类和装饰器来实现。元类是一种特殊的类,它负责创建其他类。装饰器是一种函数,它可以修改另一个函数的行为。元编程的一个常见用途是创建自定义的类。例如,你可以创建一个元类,它可以生成具有特定属性和方法的类。元编程还可以用于修改类的方法行为。例如,你可以创建一个装饰器,它可以对函数的输入和输出进行验证。元编程是一项强大的技术,它可以让你做很多有趣和有用的事情。如果你想成为一名更强大的Pyth

Geany你是最后知道的!Geany你是最后知道的!Feb 03, 2024 pm 09:24 PM

我们来了解一下吧:Geany是一个小巧的使用GTK+2开发的跨平台的开源集成开发环境,以GPL许可证分发源代码,是免费的自由软件。当前版本:1.31。该软件小巧、启动迅速,界面简洁,功能简单。它支持基本的语法高亮、代码自动完成、调用提示、插件扩展。支持文件类型:C,CPP,Java,Python,PHP,HTML,DocBook,Perl,LateX和Bash脚本。对于写多种语言的程序员来说,拥有Geany可以说是非常的方便了。知道了这么多,是不是迫不及待想要去尝试一下呢?下面让我们开始进行下载

Python程序:从字符串的右侧修剪字符串Python程序:从字符串的右侧修剪字符串Sep 08, 2023 pm 06:21 PM

在Python中,我们有一个预定义的函数rstrip()来删除右侧的字符。这意味着它将删除字符串右侧的空格。让我们举一个例子来理解如何从字符串的左侧修剪。在给定的字符串“WIRELESS”中移除右侧字符串LESS并将结果值得到为“WIRE”。在给定的字符串“kingdom”中,删除右侧的字符串dom,得到结果值为“king”。语法以下示例中使用的语法为−isspace()这是Python中预定义的方法,用于允许字符中的空白、换行符或空格。rstrip("parameterasastri

使用海龟绘制柱状图的Python程序使用海龟绘制柱状图的Python程序Aug 20, 2023 pm 04:57 PM

数据的图形表示提供了对数据复杂子结构的增强理解,帮助我们轻松解释隐藏的模式和趋势。想象一下,如果我们可以通过编程绘制类似的关系,那将是多么方便?Python提供了一个丰富的模块,专门用于执行此类操作,它被称为“turtle”。“turtle”模块是Python内置的库,允许我们在“turtle图形屏幕”上绘制图形。在本文中,我们将使用这个turtle模块创建一个条形图。理解Turtle模块Theturtlemoduleusesavirtualturtleobjecttocreategraphic

Python程序以删除字典中的空值为例Python程序以删除字典中的空值为例Sep 03, 2023 pm 04:45 PM

字典被称为集合数据类型。它们以键值对的形式存储数据。它们是有序的且可变的,即它们遵循特定的顺序并被索引。我们可以更改键的值,因此它是可操纵的或可更改的。字典不支持数据重复。每个键可以有多个与其关联的值,但单个值不能有多个键。我们可以使用字典来执行许多操作。整个机制取决于存储的值。在本文中,我们将讨论可用于从字典中删除“空值”的技术。在开始主要操作之前,我们必须对字典中的值处理有一个深入的了解。让我们快速浏览一下本文的概述。本文分为两部分-第1st部分将重点介绍“空值”的概念及其意义。在第2nd部

Python程序用于测试字符串是否只包含数字和字母Python程序用于测试字符串是否只包含数字和字母Aug 30, 2023 am 08:29 AM

在使用Python处理字符串时,经常需要验证一个字符串是否只包含数字和字母,或者是否包含其他特殊字符。字符串验证在各种场景中都非常重要,比如输入验证、数据处理和过滤。在本文中,我们将探讨一个Python程序,用于测试给定的字符串是否仅包含字母数字字符。我们将讨论有效字符串的标准,提供有效和无效字符串的示例,并介绍使用内置字符串方法解决此问题的高效方法。理解问题在我们开始解决问题之前,让我们先定义一个只包含数字和字母的有效字符串的标准-字符串不应包含任何空格或特殊字符。字符串应由字母数字字符(a-

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),