Skip to content

Commit e25ed92

Browse files
Utsav1999cclauss
authored andcommitted
Python Program to Check Krishnamurthy Number (TheAlgorithms#2248)
* Added Python Program to Check Perfet Number * CodeSpell Error Fix - 1 * Build Error Fix - 1 * Made suggested changes * Use generator expression * Added Python Program to Check Krishnamurthy Number or not * Added Python Program to Check Krishnamurthy Number * Added Python Program to Check Krishnamurthy Number or not * Build Error Fix - 1 * Build Error Fix - 2 * Fix Brackets positions * Fix Character Exceeding Error * Made Review Changes * Build Error Fix - 3 * Build Error Fix - 4 * Update krishnamurthy_number.py Co-authored-by: Christian Clauss <[email protected]>
1 parent 05016e3 commit e25ed92

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

Diff for: maths/krishnamurthy_number.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""
2+
== Krishnamurthy Number ==
3+
It is also known as Peterson Number
4+
A Krishnamurthy Number is a number whose sum of the
5+
factorial of the digits equals to the original
6+
number itself.
7+
8+
For example: 145 = 1! + 4! + 5!
9+
So, 145 is a Krishnamurthy Number
10+
"""
11+
12+
13+
def factorial(digit: int) -> int:
14+
"""
15+
>>> factorial(3)
16+
6
17+
>>> factorial(0)
18+
1
19+
>>> factorial(5)
20+
120
21+
"""
22+
23+
return 1 if digit in (0, 1) else (digit * factorial(digit - 1))
24+
25+
26+
def krishnamurthy(number: int) -> bool:
27+
"""
28+
>>> krishnamurthy(145)
29+
True
30+
>>> krishnamurthy(240)
31+
False
32+
>>> krishnamurthy(1)
33+
True
34+
"""
35+
36+
factSum = 0
37+
duplicate = number
38+
while duplicate > 0:
39+
duplicate, digit = divmod(duplicate, 10)
40+
factSum += factorial(digit)
41+
return factSum == number
42+
43+
44+
if __name__ == "__main__":
45+
print("Program to check whether a number is a Krisnamurthy Number or not.")
46+
number = int(input("Enter number: ").strip())
47+
print(
48+
f"{number} is {'' if krishnamurthy(number) else 'not '}a Krishnamurthy Number."
49+
)

Diff for: maths/perfect_number.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ def perfect(number: int) -> bool:
2525
A number at most can be divisible by the half of the number except the number
2626
itself. For example, 6 is at most can be divisible by 3 except by 6 itself.
2727
"""
28-
return sum(i for i in range(1, ((number // 2) + 1)) if number % i == 0) == number
28+
return sum(i for i in range(1, number // 2 + 1) if number % i == 0) == number
2929

3030

3131
if __name__ == "__main__":
32-
print("Program to check whether a number is a Perfect number or not.......")
32+
print("Program to check whether a number is a Perfect number or not...")
3333
number = int(input("Enter number: ").strip())
34-
print(f"{number} is {'' if perfect(number) else 'not '} a Perfect Number.")
34+
print(f"{number} is {'' if perfect(number) else 'not '}a Perfect Number.")

0 commit comments

Comments
 (0)