-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Python Program to Check Krishnamurthy Number #2248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 13 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
cc21ea1
Added Python Program to Check Perfet Number
Utsav1999 fb23a6c
CodeSpell Error Fix - 1
Utsav1999 0c6adf1
Build Error Fix - 1
Utsav1999 b0e9317
Made suggested changes
Utsav1999 c02b24e
Use generator expression
cclauss bdf71e6
Added Python Program to Check Krishnamurthy Number or not
Utsav1999 1b73ff9
Added Python Program to Check Krishnamurthy Number
Utsav1999 a2441f2
Merge branch 'utsav-python' of https://github.com/Utsav1999/Python in…
Utsav1999 48f8171
Added Python Program to Check Krishnamurthy Number or not
Utsav1999 e6cdfd9
Build Error Fix - 1
Utsav1999 9c8f150
Build Error Fix - 2
Utsav1999 d86b4d2
Fix Brackets positions
Utsav1999 348bd7f
Fix Character Exceeding Error
Utsav1999 b47618e
Made Review Changes
Utsav1999 09f0d7a
Merge branch 'master' into utsav-python
cclauss 939dcc5
Build Error Fix - 3
Utsav1999 899b558
Merge branch 'utsav-python' of https://github.com/Utsav1999/Python in…
Utsav1999 b2a23f7
Build Error Fix - 4
Utsav1999 6f48f34
Update krishnamurthy_number.py
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
""" | ||
== 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: | ||
""" | ||
function to calculate factorial of each digit | ||
""" | ||
|
||
return 1 if (digit == 0 or digit == 1) else (digit * factorial(digit - 1)) | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def krishnamurthy(number: int) -> bool: | ||
""" | ||
>>> krishnamurthy(145) | ||
True | ||
|
||
>>> krishnamurthy(240) | ||
False | ||
|
||
>>> krishnamurthy(1) | ||
True | ||
""" | ||
|
||
factSum = 0 | ||
duplicate = number | ||
while duplicate > 0: | ||
factSum += factorial(duplicate % 10) | ||
duplicate //= 10 | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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." | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
""" | ||
== Perfect Number == | ||
In number theory, a perfect number is a positive integer that is equal to the sum of | ||
its positive divisors, excluding the number itself. | ||
For example: 6 ==> divisors[1, 2, 3, 6] | ||
Excluding 6, the sum(divisors) is 1 + 2 + 3 = 6 | ||
So, 6 is a Perfect Number | ||
|
||
Other examples of Perfect Numbers: 28, 486, ... | ||
|
||
https://en.wikipedia.org/wiki/Perfect_number | ||
""" | ||
|
||
|
||
def perfect(number: int) -> bool: | ||
""" | ||
>>> perfect(27) | ||
False | ||
>>> perfect(28) | ||
True | ||
>>> perfect(29) | ||
False | ||
|
||
Start from 1 because dividing by 0 will raise ZeroDivisionError. | ||
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. | ||
""" | ||
return sum(i for i in range(1, ((number // 2) + 1)) if number % i == 0) == number | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
if __name__ == "__main__": | ||
print("Program to check whether a number is a Perfect number or not.......") | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
number = int(input("Enter number: ").strip()) | ||
print(f"{number} is {'' if perfect(number) else 'not '} a Perfect Number.") | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.