mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Python Program to Check Krishnamurthy Number (#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 <cclauss@me.com>
This commit is contained in:
parent
f760ecc73c
commit
c7a5f1673e
49
maths/krishnamurthy_number.py
Normal file
49
maths/krishnamurthy_number.py
Normal file
@ -0,0 +1,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
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def factorial(digit: int) -> int:
|
||||||
|
"""
|
||||||
|
>>> factorial(3)
|
||||||
|
6
|
||||||
|
>>> factorial(0)
|
||||||
|
1
|
||||||
|
>>> factorial(5)
|
||||||
|
120
|
||||||
|
"""
|
||||||
|
|
||||||
|
return 1 if digit in (0, 1) else (digit * factorial(digit - 1))
|
||||||
|
|
||||||
|
|
||||||
|
def krishnamurthy(number: int) -> bool:
|
||||||
|
"""
|
||||||
|
>>> krishnamurthy(145)
|
||||||
|
True
|
||||||
|
>>> krishnamurthy(240)
|
||||||
|
False
|
||||||
|
>>> krishnamurthy(1)
|
||||||
|
True
|
||||||
|
"""
|
||||||
|
|
||||||
|
factSum = 0
|
||||||
|
duplicate = number
|
||||||
|
while duplicate > 0:
|
||||||
|
duplicate, digit = divmod(duplicate, 10)
|
||||||
|
factSum += factorial(digit)
|
||||||
|
return factSum == 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 {'' if krishnamurthy(number) else 'not '}a Krishnamurthy Number."
|
||||||
|
)
|
@ -25,10 +25,10 @@ def perfect(number: int) -> bool:
|
|||||||
A number at most can be divisible by the half of the number except the number
|
A number at most can be divisible by the half of the number except the number
|
||||||
itself. For example, 6 is at most can be divisible by 3 except by 6 itself.
|
itself. For example, 6 is at most can be divisible by 3 except by 6 itself.
|
||||||
"""
|
"""
|
||||||
return sum(i for i in range(1, ((number // 2) + 1)) if number % i == 0) == number
|
return sum(i for i in range(1, number // 2 + 1) if number % i == 0) == number
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print("Program to check whether a number is a Perfect number or not.......")
|
print("Program to check whether a number is a Perfect number or not...")
|
||||||
number = int(input("Enter number: ").strip())
|
number = int(input("Enter number: ").strip())
|
||||||
print(f"{number} is {'' if perfect(number) else 'not '} a Perfect Number.")
|
print(f"{number} is {'' if perfect(number) else 'not '}a Perfect Number.")
|
||||||
|
Loading…
Reference in New Issue
Block a user