搜尋
首頁後端開發Python教學Python程式取得N個阿姆斯壯數的總和

Python程式取得N個阿姆斯壯數的總和

如果將數字的每個位數分別提升到總位數的冪,然後將這些子部分相加,得到的結果等於該數字,則稱該數字為阿姆斯特朗數。在這個Python範例中,使用兩個不同的例子,給出了找到n位阿姆斯壯數總和的方法。在範例1中,給出了計算所有3位阿姆斯壯數總和的方法。在範例2中,使用者可以在運行時決定位數。該程式使用4到6位數進行測試。

Example 1 - 找出所有3位數的阿姆斯壯數總和。

Algorithm

的中文翻譯為:

演算法

#步驟 1 - 取得所有三位數的清單。將該列表稱為listofallNums。

第 2 步 - 建立一個函數,如果計算的總和等於數字本身,則傳回一個數字的 3 次方所有數字的總和,否則將傳回-1。

步驟 3 − 對於listofallNums中的所有數字,呼叫上述函數,如果值不為-1,則將其新增至名為listofArmStrNums的清單中。

步驟 4 - 驗證 ArmStrNums 清單中的所有數字都是 3 位的 armStrong 數字。現在將所有這些 3 位 ArmStrong 數字相加。

第 5 步 - 執行程序,然後檢查結果。

Python 檔案包含此內容

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 )

查看結果 - 範例 1

要查看結果,請在命令列視窗中執行Python檔案。

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

圖1:在命令視窗中顯示結果。

Example 2:找出所有n位阿姆斯特朗數的和。

Algorithm

的中文翻譯為:

演算法

#步驟 1 - 輸入數字的值 N 並取得所有 N 位數字的清單。將該清單稱為 listofallNums。

第 2 步 - 建立一個函數,如果計算的總和等於數字本身,則傳回 N 次方所有數字的總和,否則將傳回-1。

步驟 3 − 對於listofallNums中的所有數字,呼叫上述函數,如果值不為-1,則將其新增至名為listofArmStrNums的清單中。

步驟 4 − 驗證清單listofArmStrNums中的所有數字是否為N位數的阿姆斯壯數。現在將所有這些N位數的阿姆斯壯數相加。

第 5 步 - 執行程序,然後檢查結果中是否有 4 位數字和 5 位數字。

Python 檔案包含此內容

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 )

查看結果 - 範例2

開啟cmd視窗並執行python檔案以查看結果。

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

圖 2:顯示總和和 n 位元阿姆斯壯數。

在這篇Python文章中,使用兩個不同的例子,給出了找到n位阿姆斯壯數總和的方法。在範例1中,給出了計算所有3位阿姆斯壯數總和的方法。在範例2中,使用者可以在執行時決定數字的位數。如果使用者輸入4,則給出所有4位阿姆斯特朗數及其總和。

以上是Python程式取得N個阿姆斯壯數的總和的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:tutorialspoint。如有侵權,請聯絡admin@php.cn刪除
Numpy數組與使用數組模塊創建的數組有何不同?Numpy數組與使用數組模塊創建的數組有何不同?Apr 24, 2025 pm 03:53 PM

numpyArraysareAreBetterFornumericalialoperations andmulti-demensionaldata,而learthearrayModuleSutableforbasic,內存效率段

Numpy數組的使用與使用Python中的數組模塊陣列相比如何?Numpy數組的使用與使用Python中的數組模塊陣列相比如何?Apr 24, 2025 pm 03:49 PM

numpyArraySareAreBetterForHeAvyNumericalComputing,而lelethearRayModulesiutable-usemoblemory-connerage-inderabledsswithSimpleDatateTypes.1)NumpyArsofferVerverVerverVerverVersAtility andPerformanceForlargedForlargedAtatasetSetsAtsAndAtasEndCompleXoper.2)

CTYPES模塊與Python中的數組有何關係?CTYPES模塊與Python中的數組有何關係?Apr 24, 2025 pm 03:45 PM

ctypesallowscreatingingangandmanipulatingc-stylarraysinpython.1)usectypestoInterfacewithClibrariesForperfermance.2)createc-stylec-stylec-stylarraysfornumericalcomputations.3)passarraystocfunctions foreforfunctionsforeffortions.however.however,However,HoweverofiousofmemoryManageManiverage,Pressiveo,Pressivero

在Python的上下文中定義'數組”和'列表”。在Python的上下文中定義'數組”和'列表”。Apr 24, 2025 pm 03:41 PM

Inpython,一個“列表” isaversatile,mutableSequencethatCanholdMixedDatateTypes,而“陣列” isamorememory-sepersequeSequeSequeSequeSequeRingequiringElements.1)列表

Python列表是可變還是不變的?那Python陣列呢?Python列表是可變還是不變的?那Python陣列呢?Apr 24, 2025 pm 03:37 PM

pythonlistsandArraysareBothable.1)列表Sareflexibleandsupportereceneousdatabutarelessmory-Memory-Empefficity.2)ArraysareMoremoremoremoreMemoremorememorememorememoremorememogeneSdatabutlesserversEversementime,defteringcorcttypecrecttypececeDepeceDyusagetoagetoavoavoiDerrors。

Python vs. C:了解關鍵差異Python vs. C:了解關鍵差異Apr 21, 2025 am 12:18 AM

Python和C 各有優勢,選擇應基於項目需求。 1)Python適合快速開發和數據處理,因其簡潔語法和動態類型。 2)C 適用於高性能和系統編程,因其靜態類型和手動內存管理。

Python vs.C:您的項目選擇哪種語言?Python vs.C:您的項目選擇哪種語言?Apr 21, 2025 am 12:17 AM

選擇Python還是C 取決於項目需求:1)如果需要快速開發、數據處理和原型設計,選擇Python;2)如果需要高性能、低延遲和接近硬件的控制,選擇C 。

達到python目標:每天2小時的力量達到python目標:每天2小時的力量Apr 20, 2025 am 12:21 AM

通過每天投入2小時的Python學習,可以有效提升編程技能。 1.學習新知識:閱讀文檔或觀看教程。 2.實踐:編寫代碼和完成練習。 3.複習:鞏固所學內容。 4.項目實踐:應用所學於實際項目中。這樣的結構化學習計劃能幫助你係統掌握Python並實現職業目標。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。