Python 程序:尋找強數

寫一個 Python 程序,使用 While 循環、For 循環和階乘函數查找強數,並給出一個例子。

Python 程序使用 While 循環查找強數

這個 python 程序用於一個強數,允許用戶輸入任意正整數。接下來,Python 使用 While 循環檢查給定的數字是否是強數。

# Python Program to find Strong Number

Number = int(input(" Please Enter any Number: "))
Sum = 0
Temp = Number

while(Temp > 0):
    Factorial = 1
    i = 1
    Reminder = Temp % 10

    while(i <= Reminder):
        Factorial = Factorial * i
        i = i + 1

    print("\n Factorial of %d = %d" %(Reminder, Factorial))
    Sum = Sum + Factorial
    Temp = Temp // 10

print("\n Sum of Factorials of a Given Number %d = %d" %(Number, Sum))

if (Sum == Number):
    print(" %d is a Strong Number" %Number)
else:
    print(" %d is not a Strong Number" %Number)

在這個 Python 強數示例中,首先,我們將原始值賦給 Temp 變量。它幫助我們保持我們的原始價值。

第一個 While Loop 確保給定的數字大於 0。while 循環中的語句拆分數字,並找出給定數字中各個數字的階乘。請參考 Python 數數文章了解邏輯。

第二個 While 循環(嵌套循環)查找每個數字的階乘。建議大家參考 Python 查找一個數的階乘文章,了解階乘背後的邏輯。

用戶為此 Python 程序輸入的值,以查找強數:數字= 145,總和= 0
因數 1 = 1,i = 1
溫度=數字
溫度= 145

Python 程序在循環時首先查找強名稱–第一次迭代
提醒= Temp % 10
提醒= 145 % 10 = 5

現在,它進入 Python 內部或嵌套 While 循環。這裡,它計算 5 = 120 的階乘。

總和=總和+120 => 0 + 120
總和= 120

溫度=溫度//10 => 145 //10
溫度= 14

第二次迭代
溫度= 14,總和= 120
提醒= 14 % 10 = 4

現在,它進入內部 While 循環。這裡,它計算 4 = 24 的階乘。

總和= 120 + 24
總和= 144

Temp = 14/10
Temp = 1

第三次迭代
溫度= 1,總和= 144
提醒= 1 % 10 = 0

這裡,1 的階乘是 1
和= 144 + 1
和= 145

Temp = 1/10
Temp = 0

這裡 Temp = 0,所以 while 循環條件失敗。

if(Number = = Sum)–條件檢查用戶輸入的數字是否完全等於 Sum。如果這個條件為真,那麼它就是強數,否則它不是強數。

使用 For 循環查找強數的 Python 程序

這個針對 Python 強數的程序同上。在這個 python 程序中,我們將 While 循環替換為 For 循環。

Number = int(input(" Please Enter any Number: "))
Sum = 0
Temp = Number

while(Temp > 0):
    Factorial = 1
    Reminder = Temp % 10

    for i in range(1, Reminder + 1):
        Factorial = Factorial * i

    print("Factorial of %d = %d" %(Reminder, Factorial))
    Sum = Sum + Factorial
    Temp = Temp // 10

print("\n Sum of Factorials of a Given Number %d = %d" %(Number, Sum))

if (Sum == Number):
    print(" %d is a Strong Number" %Number)
else:
    print(" %d is not a Strong Number" %Number)
 Please Enter any Number: 40585
Factorial of 5 = 120
Factorial of 8 = 40320
Factorial of 5 = 120
Factorial of 0 = 1
Factorial of 4 = 24

 Sum of Factorials of a Given Number 40585 = 40585
 40585 is a Strong Number

用階乘函數求強數的 Python 程序

這個 Python 強數程序和第一個例子一樣。然而,我們正在使用一個名為階乘的內置數學函數來尋找階乘。這種方法消除了嵌套 while 循環。

import math 
Number = int(input(" Please Enter any Number: "))
Sum = 0
Temp = Number

while(Temp > 0):
    Reminder = Temp % 10
    Factorial = math.factorial(Reminder)

    print("Factorial of %d = %d" %(Reminder, Factorial))
    Sum = Sum + Factorial
    Temp = Temp // 10

print("\n Sum of Factorials of a Given Number %d = %d" %(Number, Sum))

if (Sum == Number):
    print(" %d is a Strong Number" %Number)
else:
    print(" %d is not a Strong Number" %Number)

使用階乘輸出的 Python 強數。

 Please Enter any Number: 145
Factorial of 5 = 120
Factorial of 4 = 24
Factorial of 1 = 1

 Sum of Factorials of a Given Number 145 = 145
 145 is a Strong Number

原創文章,作者:GZ4UK,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/128733.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
GZ4UK的頭像GZ4UK
上一篇 2024-10-03 23:25
下一篇 2024-10-03 23:25

相關推薦

  • 如何查看Anaconda中Python路徑

    對Anaconda中Python路徑即conda環境的查看進行詳細的闡述。 一、使用命令行查看 1、在Windows系統中,可以使用命令提示符(cmd)或者Anaconda Pro…

    編程 2025-04-29
  • Python計算陽曆日期對應周幾

    本文介紹如何通過Python計算任意陽曆日期對應周幾。 一、獲取日期 獲取日期可以通過Python內置的模塊datetime實現,示例代碼如下: from datetime imp…

    編程 2025-04-29
  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python列表中負數的個數

    Python列表是一個有序的集合,可以存儲多個不同類型的元素。而負數是指小於0的整數。在Python列表中,我們想要找到負數的個數,可以通過以下幾個方面進行實現。 一、使用循環遍歷…

    編程 2025-04-29
  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • Python清華鏡像下載

    Python清華鏡像是一個高質量的Python開發資源鏡像站,提供了Python及其相關的開發工具、框架和文檔的下載服務。本文將從以下幾個方面對Python清華鏡像下載進行詳細的闡…

    編程 2025-04-29
  • 蝴蝶優化算法Python版

    蝴蝶優化算法是一種基於仿生學的優化算法,模仿自然界中的蝴蝶進行搜索。它可以應用於多個領域的優化問題,包括數學優化、工程問題、機器學習等。本文將從多個方面對蝴蝶優化算法Python版…

    編程 2025-04-29
  • Python字典去重複工具

    使用Python語言編寫字典去重複工具,可幫助用戶快速去重複。 一、字典去重複工具的需求 在使用Python編寫程序時,我們經常需要處理數據文件,其中包含了大量的重複數據。為了方便…

    編程 2025-04-29
  • Python程序需要編譯才能執行

    Python 被廣泛應用於數據分析、人工智能、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29
  • python強行終止程序快捷鍵

    本文將從多個方面對python強行終止程序快捷鍵進行詳細闡述,並提供相應代碼示例。 一、Ctrl+C快捷鍵 Ctrl+C快捷鍵是在終端中經常用來強行終止運行的程序。當你在終端中運行…

    編程 2025-04-29

發表回復

登錄後才能評論