在本教程中,我們將學習一個 Python 程序來查找給定的數字是否是強數。
什麼是強數?
強數是一個特殊的數,它的所有數字階乘的和應該等於數本身。
找出給定的數是否強。我們從給定的數字中挑選每個數字,並找到它的階乘,我們將對數字的每個數字都這樣做。
一旦我們得到了所有數字的階乘,那麼我們就得到階乘的和。如果和等於給定的數,那麼給定的數是強的,否則不是。
比如- 給定的數字是 145,我們要挑每個數字,求階乘 1!= 1, 4!= 24 和 5!= 120.
現在,我們將對階乘求和,我們得到 1+24+120 = 145,這與給定的數字完全相同。所以我們可以說 145 是一個強數。
我們得到了強數的邏輯。現在使用 Python 程序實現。
問題處理方式
- 要求用戶輸入一個整數。
- 使用
While循環找到數字中每個數字的階乘。 - 現在,總結所有的階乘數。
- 檢查它是否等於給定的數字。
- 列印輸出。
- 出口
樣本輸入:數= 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-tw/n/129819.html
微信掃一掃
支付寶掃一掃