Skip to content

Commit 4707fdb

Browse files
imSankocclausspre-commit-ci[bot]
authored
Add tests for Perfect_Number (TheAlgorithms#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 <[email protected]> * Added few things * Update maths/signum.py Co-authored-by: Christian Clauss <[email protected]> * 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 <[email protected]> * Added the line back * Update maths/special_numbers/perfect_number.py Co-authored-by: Christian Clauss <[email protected]> * 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 <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent b814cf3 commit 4707fdb

File tree

2 files changed

+102
-4
lines changed

2 files changed

+102
-4
lines changed

maths/perfect_number.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"""
2+
== Perfect Number ==
3+
In number theory, a perfect number is a positive integer that is equal to the sum of
4+
its positive divisors, excluding the number itself.
5+
For example: 6 ==> divisors[1, 2, 3, 6]
6+
Excluding 6, the sum(divisors) is 1 + 2 + 3 = 6
7+
So, 6 is a Perfect Number
8+
9+
Other examples of Perfect Numbers: 28, 486, ...
10+
11+
https://en.wikipedia.org/wiki/Perfect_number
12+
"""
13+
14+
15+
def perfect(number: int) -> bool:
16+
"""
17+
Check if a number is a perfect number.
18+
19+
A perfect number is a positive integer that is equal to the sum of its proper
20+
divisors (excluding itself).
21+
22+
Args:
23+
number: The number to be checked.
24+
25+
Returns:
26+
True if the number is a perfect number otherwise, False.
27+
Start from 1 because dividing by 0 will raise ZeroDivisionError.
28+
A number at most can be divisible by the half of the number except the number
29+
itself. For example, 6 is at most can be divisible by 3 except by 6 itself.
30+
Examples:
31+
>>> perfect(27)
32+
False
33+
>>> perfect(28)
34+
True
35+
>>> perfect(29)
36+
False
37+
>>> perfect(6)
38+
True
39+
>>> perfect(12)
40+
False
41+
>>> perfect(496)
42+
True
43+
>>> perfect(8128)
44+
True
45+
>>> perfect(0)
46+
False
47+
>>> perfect(-1)
48+
False
49+
>>> perfect(12.34)
50+
Traceback (most recent call last):
51+
...
52+
ValueError: number must an integer
53+
>>> perfect("Hello")
54+
Traceback (most recent call last):
55+
...
56+
ValueError: number must an integer
57+
"""
58+
if not isinstance(number, int):
59+
raise ValueError("number must an integer")
60+
if number <= 0:
61+
return False
62+
return sum(i for i in range(1, number // 2 + 1) if number % i == 0) == number
63+
64+
65+
if __name__ == "__main__":
66+
from doctest import testmod
67+
68+
testmod()
69+
print("Program to check whether a number is a Perfect number or not...")
70+
try:
71+
number = int(input("Enter a positive integer: ").strip())
72+
except ValueError:
73+
msg = "number must an integer"
74+
print(msg)
75+
raise ValueError(msg)
76+
77+
print(f"{number} is {'' if perfect(number) else 'not '}a Perfect Number.")

maths/special_numbers/perfect_number.py

+25-4
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,37 @@
1414

1515
def perfect(number: int) -> bool:
1616
"""
17+
Check if a number is a perfect number.
18+
19+
A perfect number is a positive integer that is equal to the sum of its proper
20+
divisors (excluding itself).
21+
22+
Args:
23+
number: The number to be checked.
24+
25+
Returns:
26+
True if the number is a perfect number, False otherwise.
27+
28+
Examples:
1729
>>> perfect(27)
1830
False
1931
>>> perfect(28)
2032
True
2133
>>> perfect(29)
2234
False
23-
24-
Start from 1 because dividing by 0 will raise ZeroDivisionError.
25-
A number at most can be divisible by the half of the number except the number
26-
itself. For example, 6 is at most can be divisible by 3 except by 6 itself.
35+
>>> perfect(6)
36+
True
37+
>>> perfect(12)
38+
False
39+
>>> perfect(496)
40+
True
41+
>>> perfect(8128)
42+
True
43+
>>> perfect(0)
44+
>>> perfect(-3)
45+
>>> perfect(12.34)
46+
>>> perfect("day")
47+
>>> perfect(["call"])
2748
"""
2849
return sum(i for i in range(1, number // 2 + 1) if number % i == 0) == number
2950

0 commit comments

Comments
 (0)