新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python程序:檢查數(shù)字是否是Krishnamurthy數(shù)字
創(chuàng)新互聯(lián)python教程:

亳州網(wǎng)站建設公司創(chuàng)新互聯(lián),亳州網(wǎng)站設計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為亳州成百上千提供企業(yè)網(wǎng)站建設服務。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設要多少錢,請找那個售后服務好的亳州做網(wǎng)站的公司定做!
編寫一個 Python 程序來檢查這個數(shù)字是不是一個 Krishnamurthy 數(shù)字,或者是否使用 while 循環(huán)。如果數(shù)的階乘之和等于它自己,那么任何數(shù)都是 Krishnamurthy 數(shù)。在這個 Python 例子中,我們使用 while 循環(huán)將數(shù)字分成數(shù)字。接下來,數(shù)學階乘函數(shù)找到每個數(shù)字的階乘。if 條件檢查單個數(shù)字的階乘之和是否等于一個數(shù)字。如果是真的,這是一個 Krishnamurthy 數(shù)字。
import math
Number = int(input("Enter the Number to Check Krishnamurthy Number = "))
Temp = Number
Sum = 0
while Temp > 0:
fact = 1
i = 1
rem = Temp % 10
fact = math.factorial(rem)
Sum = Sum + fact
Temp = Temp // 10
if Sum == Number:
print("\n%d is a Krishnamurthy Number." %Number)
else:
print("%d is Not a Krishnamurthy Number." %Number)
在這個 Python 示例中,我們移除了數(shù)學階乘函數(shù),并使用嵌套的 while 循環(huán)來檢查該數(shù)是否是 Krishnamurthy 數(shù)。
Number = int(input("Enter the Number to Check Krishnamurthy Number = "))
Sum = 0
Temp = Number
while Temp > 0:
fact = 1
i = 1
rem = Temp % 10
while i <= rem:
fact = fact * i
i = i + 1
print('Factorial of %d = %d' %(rem, fact))
Sum = Sum + fact
Temp = Temp // 10
print("The Sum of the Digits Factorials = %d" %Sum)
if Sum == Number:
print("\n%d is a Krishnamurthy Number." %Number)
else:
print("%d is Not a Krishnamurthy Number." %Number)
Enter the Number to Check Krishnamurthy Number = 40585
Factorial of 5 = 120
Factorial of 8 = 40320
Factorial of 5 = 120
Factorial of 0 = 1
Factorial of 4 = 24
The Sum of the Digits Factorials = 40585
40585 is a Krishnamurthy Number. 網(wǎng)站名稱:Python程序:檢查數(shù)字是否是Krishnamurthy數(shù)字
網(wǎng)頁網(wǎng)址:http://www.dlmjj.cn/article/cososhj.html


咨詢
建站咨詢
