Skip to content

docs(maths): Add comprehensive doctests for perfect_number #12810

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 20 additions & 77 deletions maths/special_numbers/perfect_number.py
Original file line number Diff line number Diff line change
@@ -1,79 +1,22 @@
"""
== 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
Determine if a number is a perfect number.
A perfect number is a positive integer that is equal to the sum of its
proper positive divisors.

More info: https://en.wikipedia.org/wiki/Perfect_number

>>> is_perfect(6)
True
>>> is_perfect(28)
True
>>> is_perfect(496)
True
>>> is_perfect(10)
False
>>> is_perfect(1)
False
>>> is_perfect(0)
False
>>> is_perfect(-6)
False
"""


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, False otherwise.

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 be an integer
>>> perfect("Hello")
Traceback (most recent call last):
...
ValueError: number must be an integer
"""
if not isinstance(number, int):
raise ValueError("number must be 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 be an integer"
print(msg)
raise ValueError(msg)

print(f"{number} is {'' if perfect(number) else 'not '}a Perfect Number.")
Loading