Python 中的強數

在本教程中,我們將學習一個 Python 程序來查找給定的數字是否是強數。

什麼是強數?

強數是一個特殊的數,它的所有數字階乘的和應該等於數本身。

找出給定的數是否強。我們從給定的數字中挑選每個數字,並找到它的階乘,我們將對數字的每個數字都這樣做。

一旦我們得到了所有數字的階乘,那麼我們就得到階乘的和。如果和等於給定的數,那麼給定的數是強的,否則不是。

比如- 給定的數字是 145,我們要挑每個數字,求階乘 1!= 1, 4!= 24 和 5!= 120.

現在,我們將對階乘求和,我們得到 1+24+120 = 145,這與給定的數字完全相同。所以我們可以說 145 是一個強數。

我們得到了強數的邏輯。現在使用 Python 程序實現。

問題處理方式

  1. 要求用戶輸入一個整數。
  2. 使用 While循環找到數字中每個數字的階乘。
  3. 現在,總結所有的階乘數。
  4. 檢查它是否等於給定的數字。
  5. 打印輸出。
  6. 出口

樣本輸入:數= 132

樣本輸出:給定的數字不是一個強數

說明: 1!+ 3!+ 2!= 9,不等於 132

樣本輸入:數= 145

樣本輸出:給定的數字是一個強數。

Python 程序:尋找強數

下面是 Python 程序打印給定數字是強還是非強的代碼。

示例-


# Variable to store sum of the numbers
sum=0
# Ask user to enter the number
num=int(input("Enter a number:"))
# temporary variable  store copy of the original number
temp=num
# Using while loop
while(num):
    # intialize with 1
    i=1
    # fact variable with 1
    fact=1
    rem=num%10
    while(i<=rem):
        fact=fact*i   # Find factorial of each number
        i=i+1
    sum=sum+fact
    num=num//10
if(sum==temp):
    print("Given number is a strong number")
else:
    print("Given number is not a strong number")

輸出:

Enter a number: 145
Given number is a strong number.

說明:

在上面的代碼中

  • 我們聲明了一個可變整數值 num 用於輸入一個數字。
  • 用零定義和變量。
  • 臨時變量的 num 值的副本。
  • 在第一個 While循環中,確保給定的數字大於 0。
  • While循環中,拆分數字並分配變量以找到每個數字的階乘。
  • 在第二個 While循環(嵌套 While循環)中,找到每個數字的階乘。

假設用戶輸入值= 145,sum = 0

分配初始值


i = 0
fact = 0
temp = num
temp = 145

現在理解循環迭代。第一次迭代


rem = temp % 10
rem = 145 % 10 = 5

現在,我們進入了嵌套的 While循環。它計算出 5 的階乘是 120。


sum = sum + 120> 0+120
sum = 120
temp = temp//10 = 14
temp = 14

第二次迭代


temp = 14,
sum = 120
rem = 14 % 10 = 4

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


sum = 120 + 24
sum = 144

temp = 14//10   
temp = 1

第三次迭代


temp = 1 
sum = 144
rem = 1 % 10 = 0

1 的階乘是 1


sum = 144 + 1
sum = 145
temp = 1 / 10
temp = 0

這裡溫度= 0,所以 While循環條件失敗。

如果(num == sum)現在,我們檢查用戶輸入的數字是否正好等於 sum。如果這個條件返回真,那麼它是強數,否則它不是強數。

我們已經使用 While循環完成了程序。我們也可以使用進行循環來查找給定的數字是否強。

使用 for循環的強名稱

我們還可以找到用於循環的強數。邏輯與上述程序相同,While循環由 for循環代替。

示例-


# Python Program to find Strong Number
num = int(input(" Enter the Number:"))
sum = 0
temp = num

while(temp > 0):
    fact = 1
    rem = temp % 10

    for i in range(1, rem + 1):
        fact = fact * i

    print("Factorial of %d = %d" %(rem, fact))
    sum = sum + fact
    temp = temp // 10

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

if (sum == num):
    print(" The given number is a Strong Number")
else:
    print(" The given number is not a Strong Number")

輸出:

Enter the Number:145
Factorial of 5 = 120
Factorial of 4 = 24
Factorial of 1 = 1
Sum of Factorials of a Given Number 145 = 145
The given number is a Strong Number

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

Python math模塊提供內置的math模塊。通過使用這種方法,我們可以省略使用嵌套 While循環。

示例-


# Python Program to find Strong Number

import math
num = int(input(" Enter the Number:"))
sum = 0
temp = num

while(temp > 0):
    rem = temp % 10
    fact = math.factorial(rem)  # Using the buitlt-in factorial() function

    print("Factorial of %d = %d" %(rem, fact))
    sum = sum + fact
    temp = temp // 10

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

if (sum == num):
    print(" The given number is a Strong Number")
else:
    print(" The given number is not a Strong Number") 

輸出:

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

 Sum of Factorials of a Given Number 145 = 145
 The given number is a Strong Number

解釋-

在上面的代碼中,

  • 我們使用了階乘()函數,並傳遞了一個提醒作為參數。

  • While循環的第一次迭代中,它返回提醒 5 並傳遞給階乘 5。

  • 它將一直持續到溫度值大於零。我們不需要使用另一個 While循環。

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

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

相關推薦

  • Python周杰倫代碼用法介紹

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

    編程 2025-04-29
  • 如何查看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中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

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

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

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

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

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

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

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

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

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

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

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

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

    編程 2025-04-29

發表回復

登錄後才能評論