- Notifications
You must be signed in to change notification settings - Fork 46.7k
/
Copy pathkrishnamurthy_number.py
49 lines (40 loc) · 1.05 KB
/
krishnamurthy_number.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
== Krishnamurthy Number ==
It is also known as Peterson Number
A Krishnamurthy Number is a number whose sum of the
factorial of the digits equals to the original
number itself.
For example: 145 = 1! + 4! + 5!
So, 145 is a Krishnamurthy Number
"""
deffactorial(digit: int) ->int:
"""
>>> factorial(3)
6
>>> factorial(0)
1
>>> factorial(5)
120
"""
return1ifdigitin (0, 1) else (digit*factorial(digit-1))
defkrishnamurthy(number: int) ->bool:
"""
>>> krishnamurthy(145)
True
>>> krishnamurthy(240)
False
>>> krishnamurthy(1)
True
"""
fact_sum=0
duplicate=number
whileduplicate>0:
duplicate, digit=divmod(duplicate, 10)
fact_sum+=factorial(digit)
returnfact_sum==number
if__name__=="__main__":
print("Program to check whether a number is a Krisnamurthy Number or not.")
number=int(input("Enter number: ").strip())
print(
f"{number} is {''ifkrishnamurthy(number) else'not '}a Krishnamurthy Number."
)