Skip to content

Commit 8d3b579

Browse files
Add function to check abundant numbers
Introduces is_abundant_number to determine if a number is abundant by checking if the sum of its proper divisors exceeds the number itself. Includes doctests and input validation.
1 parent 3cea941 commit 8d3b579

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""
2+
A number n is said to be an Abundant number if
3+
the sum of its proper divisors is greater than the number itself.
4+
5+
Examples of Abundant Numbers: 12, 18, 20, 24, 30, 36, 40, 42, 48, 54, ...
6+
"""
7+
8+
def is_abundant_number(number: int) -> bool:
9+
"""
10+
This function takes an integer number as input.
11+
Returns True if the number is an abundant number.
12+
13+
>>> is_abundant_number(-1)
14+
False
15+
>>> is_abundant_number(0)
16+
False
17+
>>> is_abundant_number(12)
18+
True
19+
>>> is_abundant_number(18)
20+
True
21+
>>> is_abundant_number(28)
22+
False
23+
>>> is_abundant_number(20)
24+
True
25+
>>> is_abundant_number(6)
26+
False
27+
>>> is_abundant_number(1)
28+
False
29+
>>> is_abundant_number(945)
30+
True
31+
>>> is_abundant_number(28.0)
32+
Traceback (most recent call last):
33+
...
34+
TypeError: Input value of [number=28.0] must be an integer
35+
"""
36+
if not isinstance(number, int):
37+
msg = f"Input value of [number={number}] must be an integer"
38+
raise TypeError(msg)
39+
if number < 1:
40+
return False
41+
42+
divisor_sum = 1 # 1 is always a proper divisor
43+
for i in range(2, int(number ** 0.5) + 1):
44+
if number % i == 0:
45+
divisor_sum += i
46+
if i != number // i:
47+
divisor_sum += number // i
48+
return divisor_sum > number
49+
50+
if __name__ == "__main__":
51+
import doctest
52+
53+
doctest.testmod()

0 commit comments

Comments
 (0)