-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Add tests for Perfect_Number #10745
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
Add tests for Perfect_Number #10745
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
ff09802
Added new tests!
imSanko c605c66
[ADD]: Inproved Tests
imSanko 9780df1
fixed
imSanko 35aca47
Removed spaces
imSanko 0c5eb56
Changed the file name
imSanko 3c7ee47
Added Changes
imSanko 07fb300
changed the code and kept the test cases
imSanko 290efe8
changed the code and kept the test cases
imSanko 0dd5a3c
missed the line
imSanko dc817c6
removed spaces
imSanko 3e51abd
Merge branch 'TheAlgorithms:master' into master
imSanko 681e02a
Update power_using_recursion.py
cclauss ac4b1f0
Merge branch 'TheAlgorithms:master' into master
imSanko c53e78c
Added new tests in Signum
imSanko d89a0b9
Few things added
imSanko 6d2f1c3
Removed few stuff and added few changes
imSanko 786f1a6
Fixed few things
imSanko e31687b
Reverted the function
imSanko 9daac1a
Update maths/signum.py
imSanko 27b9c8a
Added few things
imSanko 6714d15
Update maths/signum.py
imSanko b4ea39f
Added the type hint back
imSanko 0b0a390
Update signum.py
cclauss 05476d6
Added NEW tests for Perfect_Number
imSanko f4f6c62
Merge branch 'master' of https://github.com/imSanko/Python
imSanko 1d42ec9
Update maths/special_numbers/perfect_number.py
imSanko 75cb826
Added the line back
imSanko 974130b
Update maths/special_numbers/perfect_number.py
imSanko e1a5b17
Fixed a space
imSanko 6ad0257
Updated
imSanko 48c7f4b
Reverted changes
imSanko 6d23cbb
Added the old code and FIXED few LINES
imSanko 5f1b8ae
Fixed few things
imSanko 54fd69f
Changed Test CASES
imSanko 01e81c4
Update perfect_number.py
cclauss a355f82
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 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,77 @@ | ||
""" | ||
== 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: | ||
""" | ||
Check if a number is a perfect number. | ||
|
||
A perfect number is a positive integer that is equal to the sum of its proper | ||
divisors (excluding itself). | ||
|
||
Args: | ||
number: The number to be checked. | ||
|
||
Returns: | ||
True if the number is a perfect number otherwise, 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. | ||
Examples: | ||
>>> perfect(27) | ||
False | ||
>>> perfect(28) | ||
True | ||
>>> perfect(29) | ||
False | ||
>>> perfect(6) | ||
True | ||
>>> perfect(12) | ||
False | ||
>>> perfect(496) | ||
True | ||
>>> perfect(8128) | ||
True | ||
>>> perfect(0) | ||
False | ||
>>> perfect(-1) | ||
False | ||
>>> perfect(12.34) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: number must an integer | ||
>>> perfect("Hello") | ||
Traceback (most recent call last): | ||
... | ||
ValueError: number must an integer | ||
""" | ||
if not isinstance(number, int): | ||
raise ValueError("number must an integer") | ||
if number <= 0: | ||
return False | ||
return sum(i for i in range(1, number // 2 + 1) if number % i == 0) == number | ||
|
||
|
||
if __name__ == "__main__": | ||
from doctest import testmod | ||
|
||
testmod() | ||
print("Program to check whether a number is a Perfect number or not...") | ||
try: | ||
number = int(input("Enter a positive integer: ").strip()) | ||
except ValueError: | ||
msg = "number must an integer" | ||
print(msg) | ||
raise ValueError(msg) | ||
|
||
print(f"{number} is {'' if perfect(number) else 'not '}a Perfect Number.") |
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
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.