Skip to content

Commit 0ce22f2

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 81e5df5 commit 0ce22f2

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

maths/aliquot_sum.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,31 @@
22
import doctest
33
from typing import overload
44

5+
56
@overload
67
def aliquot_sum(input_num: int) -> int: ...
78
@overload
89
def aliquot_sum(input_num: int, return_factors: bool) -> tuple[int, list[int]]: ...
910

11+
1012
def aliquot_sum(
1113
input_num: int, return_factors: bool = False
1214
) -> int | tuple[int, list[int]]:
1315
"""
14-
Calculates the aliquot sum of a positive integer.
16+
Calculates the aliquot sum of a positive integer.
1517
The aliquot sum is the sum of all proper divisors of a number.
16-
18+
1719
Args:
1820
input_num: Positive integer
1921
return_factors: If True, returns (sum, sorted_factor_list)
20-
22+
2123
Returns:
2224
Aliquot sum or (sum, factors) if return_factors=True
23-
25+
2426
Raises:
2527
TypeError: If input not integer
2628
ValueError: If input not positive
27-
29+
2830
Examples:
2931
>>> aliquot_sum(15)
3032
9
@@ -36,16 +38,16 @@ def aliquot_sum(
3638
raise TypeError("Input must be an integer")
3739
if input_num <= 0:
3840
raise ValueError("Input must be positive integer")
39-
41+
4042
# Special case: 1 has no proper divisors
4143
if input_num == 1:
4244
return (0, []) if return_factors else 0
43-
45+
4446
# Initialize factors and total
4547
factors = [1]
4648
total = 1
4749
sqrt_num = int(input_num**0.5)
48-
50+
4951
# Find factors efficiently
5052
for divisor in range(2, sqrt_num + 1):
5153
if input_num % divisor == 0:
@@ -55,17 +57,18 @@ def aliquot_sum(
5557
if complement != divisor:
5658
factors.append(complement)
5759
total += complement
58-
60+
5961
factors.sort()
6062
return (total, factors) if return_factors else total
6163

64+
6265
def classify_number(n: int) -> str:
6366
"""
6467
Classifies number based on aliquot sum:
6568
- Perfect: sum = number
6669
- Abundant: sum > number
6770
- Deficient: sum < number
68-
71+
6972
Examples:
7073
>>> classify_number(6)
7174
'Perfect'
@@ -76,24 +79,25 @@ def classify_number(n: int) -> str:
7679
raise ValueError("Input must be positive integer")
7780
if n == 1:
7881
return "Deficient"
79-
82+
8083
s = aliquot_sum(n) # Always returns int
8184
if s == n:
8285
return "Perfect"
8386
return "Abundant" if s > n else "Deficient"
8487

88+
8589
if __name__ == "__main__":
8690
doctest.testmod()
87-
91+
8892
print("Aliquot sum of 28:", aliquot_sum(28))
89-
93+
9094
# Handle tuple return explicitly
9195
result = aliquot_sum(28, True)
9296
if isinstance(result, tuple):
9397
print("Factors of 28:", result[1])
94-
98+
9599
print("Classification of 28:", classify_number(28))
96-
100+
97101
# Large number test
98102
try:
99103
print("Aliquot sum for 10^9:", aliquot_sum(10**9))

0 commit comments

Comments
 (0)