您的位置:

Python 程序:检查数字是否是 Krishnamurthy 数字

编写一个 Python 程序来检查这个数字是不是一个 Krishnamurthy 数字,或者是否使用 while 循环。如果数的阶乘之和等于它自己,那么任何数都是 Krishnamurthy 数。在这个 Python 例子中,我们使用 while 循环将数字分成数字。接下来,数学阶乘函数找到每个数字的阶乘。if 条件检查单个数字的阶乘之和是否等于一个数字。如果是真的,这是一个 Krishnamurthy 数字。

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 示例中,我们移除了数学阶乘函数,并使用嵌套的 while 循环来检查该数是否是 Krishnamurthy 数。

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.