Python程式列印給定列表中所有強數


如果一個數的各位數字的階乘之和等於該數本身,則稱該數為強數。在這篇Python文章中,我們將透過三個不同的例子,介紹在列表中查詢強數的不同方法。在示例1中,我們使用一個包含預先計算好的數字階乘的列表來計算一個數中所有數字的階乘之和,然後檢查該數是否為強數。在示例2中,我們使用一個函式計算0到9所有數字的階乘,並將它們儲存在一個列表中,然後使用該列表計算一個數中所有數字的階乘之和,並檢查該數是否為強數。在示例3中,我們直接從待檢查的數中分離出各個數字,計算它們的階乘並將其加在一起,無需使用單獨的階乘列表。

示例1 - 使用預先計算好的各個數字階乘的單獨列表列印給定列表中的所有強數

演算法

步驟1 - 獲取一個整數列表。確保此測試列表至少包含一個強數。建立一個名為numType的函式來檢查數字是否為強數。

步驟2 - 建立一個單獨的列表,其中包含0到9的預先計算好的階乘。

步驟3 - 對於測試列表中的每個數字,使用numType函式,首先分離該數字的各位數字,然後從階乘列表中選擇預先計算好的階乘。

步驟4 - 將這些階乘的和加起來。如果該數等於總和,則列印該數。

步驟5 - 執行程式並檢查結果。

Python檔案包含這些內容

list1=[300, 40585, 500, 460, 3, 2, 89, 1, 145, 260, 630]
print("Sample List: ", list1)
#List having listoffactorials of numbers from 1 to 9
listoffactorials = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880]

def numType(num):
   numstr = str(num)
   sum=0

   for digit in numstr:
      sum += listoffactorials[int(digit)]

   if sum == num:
      return "isStrong"
   else:
      return "notStrong"    


for item in list1:
   if (numType(item) == "isStrong"):
      print("Strong Number: ", item)  

檢視結果

要檢視結果,請在cmd視窗中執行Python檔案。

Sample List:  [300, 40585, 500, 460, 3, 2, 89, 1, 145, 260, 630]
Strong Number:  40585
Strong Number:  2
Strong Number:  1
Strong Number:  145

示例2:透過使用函式計算各個數字的階乘並建立它們的單獨列表,列印給定列表中的所有強數

演算法

步驟1 - 指定一個整數列表。確保此示例列表至少包含一個強數。建立一個名為numType的函式來檢查數字是否為強數。

步驟2 - 建立一個calcFactorial()函式,並對0到9的數字呼叫它,建立一個單獨的列表,其中包含0到9的數字的階乘。

步驟3 - 對於測試列表中的每個數字,使用numType函式,首先分離該數字的各位數字,然後從階乘列表中選擇計算出的階乘。使用數字的字串形式來分離數字,然後將其轉換回整數。

步驟4 - 將這些階乘的和加起來。如果該數等於總和,則列印該強數。

步驟5 - 執行程式並檢查結果。

Python檔案包含這些內容

listoffactorials=[]

def calcFactorial(num):
   factorial=1
   if num < 0:
      print("Error, the number is negative")
   elif num > 0:
      for item in range(1 , num + 1):
         factorial = factorial*item
      return  factorial  
   else:
      return 1
   
for item in range(0, 10):
   listoffactorials.append(calcFactorial(item))

print("Calculated digits factorial list:", listoffactorials)    

def numType(num):
   numstr = str(num)
   sum=0

   for digit in numstr:
      sum += listoffactorials[int(digit)]

   if sum == num:
      return "isStrong"
   else:
      return "notStrong"    
 
list1=[300, 40585, 500, 460, 3, 2, 89, 1, 145, 260, 630]
print("Sample List: ", list1)
for item in list1:
   if (numType(item) == "isStrong"):
      print("Strong Number: ", item)

檢視結果

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

Calculated digits factorial list: [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880]
Sample List:  [300, 40585, 500, 460, 3, 2, 89, 1, 145, 260, 630]
Strong Number:  40585
Strong Number:  2
Strong Number:  1
Strong Number:  145   

示例3:在過程中不使用單獨的各個數字階乘列表列印給定列表中的所有強數

演算法

步驟1 - 建立一個整數列表。確保此示例列表至少包含一個強數。

步驟2 - 建立一個calcFactorial()函式來計算指定數字的階乘。

步驟3 - 對於測試列表中的每個數字,首先分離該數字的各位數字,並計算其階乘。然後將此階乘新增到總和中。使用數字的整數形式透過除以十和檢查餘數來分離數字。

步驟4 - 檢查該數是否等於總和,然後列印該強數。

步驟5 - 執行程式並檢查結果。

Python檔案包含這些內容

list1=[300, 40585, 500, 460, 3, 2, 89, 1, 145, 260, 630]
print("Sample List: ", list1)
def calcFactorial(num):
   factorial=1
   if num < 0:
      print("Error, the number is negative")
   elif num > 0:
      for item in range(1 , num + 1):
         factorial = factorial*item
      return  factorial  
   else:
      return 1

for item in list1:
   tempItem = item
   total = 0
   while(tempItem):
      rem = tempItem % 10
      total += calcFactorial(rem)
      tempItem = tempItem // 10
   if(total == item):
      print("Strong Number: ", item)
   else:
      print("Not a Strong Number: ", item)

檢視結果

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

Sample List:  [300, 40585, 500, 460, 3, 2, 89, 1, 145, 260, 630]
Not a Strong Number:  300
Strong Number:  40585
Not a Strong Number:  500
Not a Strong Number:  460
Not a Strong Number:  3
Strong Number:  2
Not a Strong Number:  89
Strong Number:  1
Strong Number:  145
Not a Strong Number:  260
Not a Strong Number:  630 

在這篇Python文章中,我們透過三個不同的例子,介紹了從給定的整數列表中查詢強數的方法。在示例1中,該方法使用預先計算好的階乘列表來計算各個數字的階乘及其和。在示例2中,我們沒有使用預先計算好的列表,而是使用一個函式來計算階乘列表。在示例1和2中,都使用了數字的字串形式來查詢各個數字。在示例3中,我們使用除法取餘的方法分離數字,然後計算它們的階乘並將其加在一起。

更新於:2023年8月8日

217 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.