Skip to content

Commit 81e5df5

Browse files
authored
Update aliquot_sum.py
1 parent f3a0ef6 commit 81e5df5

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

maths/aliquot_sum.py

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,30 @@
11
from __future__ import annotations
2-
32
import doctest
4-
from typing import overload, Tuple
5-
3+
from typing import overload
64

75
@overload
86
def aliquot_sum(input_num: int) -> int: ...
97
@overload
10-
def aliquot_sum(input_num: int, return_factors: bool) -> Tuple[int, list[int]]: ...
11-
8+
def aliquot_sum(input_num: int, return_factors: bool) -> tuple[int, list[int]]: ...
129

1310
def aliquot_sum(
1411
input_num: int, return_factors: bool = False
15-
) -> int | Tuple[int, list[int]]:
12+
) -> int | tuple[int, list[int]]:
1613
"""
17-
Calculates the aliquot sum of a positive integer.
14+
Calculates the aliquot sum of a positive integer.
1815
The aliquot sum is the sum of all proper divisors of a number.
19-
16+
2017
Args:
2118
input_num: Positive integer
2219
return_factors: If True, returns (sum, sorted_factor_list)
23-
20+
2421
Returns:
2522
Aliquot sum or (sum, factors) if return_factors=True
26-
23+
2724
Raises:
2825
TypeError: If input not integer
2926
ValueError: If input not positive
30-
27+
3128
Examples:
3229
>>> aliquot_sum(15)
3330
9
@@ -39,16 +36,16 @@ def aliquot_sum(
3936
raise TypeError("Input must be an integer")
4037
if input_num <= 0:
4138
raise ValueError("Input must be positive integer")
42-
39+
4340
# Special case: 1 has no proper divisors
4441
if input_num == 1:
4542
return (0, []) if return_factors else 0
46-
43+
4744
# Initialize factors and total
4845
factors = [1]
4946
total = 1
5047
sqrt_num = int(input_num**0.5)
51-
48+
5249
# Find factors efficiently
5350
for divisor in range(2, sqrt_num + 1):
5451
if input_num % divisor == 0:
@@ -58,18 +55,17 @@ def aliquot_sum(
5855
if complement != divisor:
5956
factors.append(complement)
6057
total += complement
61-
58+
6259
factors.sort()
6360
return (total, factors) if return_factors else total
6461

65-
6662
def classify_number(n: int) -> str:
6763
"""
6864
Classifies number based on aliquot sum:
6965
- Perfect: sum = number
7066
- Abundant: sum > number
7167
- Deficient: sum < number
72-
68+
7369
Examples:
7470
>>> classify_number(6)
7571
'Perfect'
@@ -80,25 +76,24 @@ def classify_number(n: int) -> str:
8076
raise ValueError("Input must be positive integer")
8177
if n == 1:
8278
return "Deficient"
83-
79+
8480
s = aliquot_sum(n) # Always returns int
8581
if s == n:
8682
return "Perfect"
8783
return "Abundant" if s > n else "Deficient"
8884

89-
9085
if __name__ == "__main__":
9186
doctest.testmod()
92-
87+
9388
print("Aliquot sum of 28:", aliquot_sum(28))
94-
89+
9590
# Handle tuple return explicitly
9691
result = aliquot_sum(28, True)
9792
if isinstance(result, tuple):
9893
print("Factors of 28:", result[1])
99-
94+
10095
print("Classification of 28:", classify_number(28))
101-
96+
10297
# Large number test
10398
try:
10499
print("Aliquot sum for 10^9:", aliquot_sum(10**9))

0 commit comments

Comments
 (0)