Python/maths/special_numbers/perfect_number.py
Saptadeep Banerjee 4707fdb0f2
Add tests for Perfect_Number (#10745)
* Added new tests!

* [ADD]: Inproved Tests

* fixed

* Removed spaces

* Changed the file name

* Added Changes

* changed the code and kept the test cases

* changed the code and kept the test cases

* missed the line

* removed spaces

* Update power_using_recursion.py

* Added new tests in Signum

* Few things added

* Removed few stuff and added few changes

* Fixed few things

* Reverted the function

* Update maths/signum.py

Co-authored-by: Christian Clauss <cclauss@me.com>

* Added few things

* Update maths/signum.py

Co-authored-by: Christian Clauss <cclauss@me.com>

* Added the type hint back

* Update signum.py

* Added NEW tests for Perfect_Number

* Update maths/special_numbers/perfect_number.py

Co-authored-by: Christian Clauss <cclauss@me.com>

* Added the line back

* Update maths/special_numbers/perfect_number.py

Co-authored-by: Christian Clauss <cclauss@me.com>

* Fixed a space

* Updated

* Reverted changes

* Added the old code and FIXED few LINES

* Fixed few things

* Changed Test CASES

* Update perfect_number.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: Christian Clauss <cclauss@me.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-10-22 00:05:37 +02:00

56 lines
1.3 KiB
Python

"""
== 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, False otherwise.
Examples:
>>> perfect(27)
False
>>> perfect(28)
True
>>> perfect(29)
False
>>> perfect(6)
True
>>> perfect(12)
False
>>> perfect(496)
True
>>> perfect(8128)
True
>>> perfect(0)
>>> perfect(-3)
>>> perfect(12.34)
>>> perfect("day")
>>> perfect(["call"])
"""
return sum(i for i in range(1, number // 2 + 1) if number % i == 0) == number
if __name__ == "__main__":
print("Program to check whether a number is a Perfect number or not...")
number = int(input("Enter number: ").strip())
print(f"{number} is {'' if perfect(number) else 'not '}a Perfect Number.")