Skip to content

Commit f3a0ef6

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

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
@@ -3,29 +3,31 @@
33
import doctest
44
from typing import overload, Tuple
55

6+
67
@overload
78
def aliquot_sum(input_num: int) -> int: ...
89
@overload
910
def aliquot_sum(input_num: int, return_factors: bool) -> Tuple[int, list[int]]: ...
1011

12+
1113
def aliquot_sum(
1214
input_num: int, return_factors: bool = False
1315
) -> int | Tuple[int, list[int]]:
1416
"""
15-
Calculates the aliquot sum of a positive integer.
17+
Calculates the aliquot sum of a positive integer.
1618
The aliquot sum is the sum of all proper divisors of a number.
17-
19+
1820
Args:
1921
input_num: Positive integer
2022
return_factors: If True, returns (sum, sorted_factor_list)
21-
23+
2224
Returns:
2325
Aliquot sum or (sum, factors) if return_factors=True
24-
26+
2527
Raises:
2628
TypeError: If input not integer
2729
ValueError: If input not positive
28-
30+
2931
Examples:
3032
>>> aliquot_sum(15)
3133
9
@@ -37,16 +39,16 @@ def aliquot_sum(
3739
raise TypeError("Input must be an integer")
3840
if input_num <= 0:
3941
raise ValueError("Input must be positive integer")
40-
42+
4143
# Special case: 1 has no proper divisors
4244
if input_num == 1:
4345
return (0, []) if return_factors else 0
44-
46+
4547
# Initialize factors and total
4648
factors = [1]
4749
total = 1
4850
sqrt_num = int(input_num**0.5)
49-
51+
5052
# Find factors efficiently
5153
for divisor in range(2, sqrt_num + 1):
5254
if input_num % divisor == 0:
@@ -56,17 +58,18 @@ def aliquot_sum(
5658
if complement != divisor:
5759
factors.append(complement)
5860
total += complement
59-
61+
6062
factors.sort()
6163
return (total, factors) if return_factors else total
6264

65+
6366
def classify_number(n: int) -> str:
6467
"""
6568
Classifies number based on aliquot sum:
6669
- Perfect: sum = number
6770
- Abundant: sum > number
6871
- Deficient: sum < number
69-
72+
7073
Examples:
7174
>>> classify_number(6)
7275
'Perfect'
@@ -77,24 +80,25 @@ def classify_number(n: int) -> str:
7780
raise ValueError("Input must be positive integer")
7881
if n == 1:
7982
return "Deficient"
80-
83+
8184
s = aliquot_sum(n) # Always returns int
8285
if s == n:
8386
return "Perfect"
8487
return "Abundant" if s > n else "Deficient"
8588

89+
8690
if __name__ == "__main__":
8791
doctest.testmod()
88-
92+
8993
print("Aliquot sum of 28:", aliquot_sum(28))
90-
94+
9195
# Handle tuple return explicitly
9296
result = aliquot_sum(28, True)
9397
if isinstance(result, tuple):
9498
print("Factors of 28:", result[1])
95-
99+
96100
print("Classification of 28:", classify_number(28))
97-
101+
98102
# Large number test
99103
try:
100104
print("Aliquot sum for 10^9:", aliquot_sum(10**9))

0 commit comments

Comments
 (0)