Skip to content

Commit dfd6c97

Browse files
authored
Update aliquot_sum.py
1 parent 596281d commit dfd6c97

1 file changed

Lines changed: 37 additions & 37 deletions

File tree

maths/aliquot_sum.py

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
1+
from typing import overload, Tuple
2+
3+
@overload
4+
def aliquot_sum(input_num: int, return_factors: bool = False) -> int: ...
5+
@overload
6+
def aliquot_sum(input_num: int, return_factors: bool = True) -> Tuple[int, list[int]]: ...
7+
18
def aliquot_sum(
29
input_num: int, return_factors: bool = False
3-
) -> int | tuple[int, list[int]]:
10+
) -> int | Tuple[int, list[int]]:
411
"""
512
Calculates the aliquot sum of a positive integer. The aliquot sum is defined as
613
the sum of all proper divisors of a number (all divisors except the number itself).
7-
14+
815
This implementation uses an optimized O(sqrt(n)) algorithm for efficiency.
9-
16+
1017
Args:
1118
input_num: Positive integer to calculate aliquot sum for
1219
return_factors: If True, returns tuple (aliquot_sum, sorted_factor_list)
13-
20+
1421
Returns:
1522
Aliquot sum if return_factors=False
1623
Tuple (aliquot_sum, sorted_factor_list) if return_factors=True
17-
24+
1825
Raises:
1926
TypeError: If input is not an integer
2027
ValueError: If input is not positive
21-
28+
2229
Examples:
2330
>>> aliquot_sum(15)
2431
9
@@ -30,42 +37,41 @@ def aliquot_sum(
3037
# Validate input type - must be integer
3138
if not isinstance(input_num, int):
3239
raise TypeError("Input must be an integer")
33-
40+
3441
# Validate input value - must be positive
3542
if input_num <= 0:
3643
raise ValueError("Input must be positive integer")
37-
44+
3845
# Special case: 1 has no proper divisors
3946
if input_num == 1:
40-
# Return empty factor list if requested
4147
return (0, []) if return_factors else 0
42-
48+
4349
# Initialize factors list with 1 (always a divisor)
44-
factors = [1]
50+
factors = [1]
4551
total = 1 # Start sum with 1
46-
52+
4753
# Calculate square root as optimization boundary
4854
sqrt_num = int(input_num**0.5)
49-
55+
5056
# Iterate potential divisors from 2 to square root
5157
for divisor in range(2, sqrt_num + 1):
5258
# Check if divisor is a factor
5359
if input_num % divisor == 0:
5460
# Add divisor to factors list
5561
factors.append(divisor)
5662
total += divisor
57-
63+
5864
# Calculate complement (pair factor)
5965
complement = input_num // divisor
60-
66+
6167
# Avoid duplicate for perfect squares
62-
if complement != divisor:
68+
if complement != divisor:
6369
factors.append(complement)
6470
total += complement
65-
71+
6672
# Sort factors for consistent output
6773
factors.sort()
68-
74+
6975
# Return based on return_factors flag
7076
return (total, factors) if return_factors else total
7177

@@ -76,16 +82,16 @@ def classify_number(n: int) -> str:
7682
- Perfect: aliquot sum = number
7783
- Abundant: aliquot sum > number
7884
- Deficient: aliquot sum < number
79-
85+
8086
Args:
8187
n: Positive integer to classify
82-
88+
8389
Returns:
8490
Classification string ("Perfect", "Abundant", or "Deficient")
85-
91+
8692
Raises:
8793
ValueError: If input is not positive
88-
94+
8995
Examples:
9096
>>> classify_number(6)
9197
'Perfect'
@@ -97,14 +103,14 @@ def classify_number(n: int) -> str:
97103
# Validate input
98104
if n <= 0:
99105
raise ValueError("Input must be positive integer")
100-
106+
101107
# Special case: 1 is always deficient
102108
if n == 1:
103109
return "Deficient"
104-
105-
# Calculate aliquot sum (explicitly request integer-only result)
106-
s = aliquot_sum(n, return_factors=False)
107-
110+
111+
# Calculate aliquot sum (using only the integer version)
112+
s = aliquot_sum(n) # Default returns only integer
113+
108114
# Determine classification
109115
if s == n:
110116
return "Perfect"
@@ -113,21 +119,15 @@ def classify_number(n: int) -> str:
113119

114120
if __name__ == "__main__":
115121
import doctest
116-
122+
117123
# Run embedded doctests for verification
118124
doctest.testmod()
119-
125+
120126
# Additional demonstration examples
121127
print("Aliquot sum of 28:", aliquot_sum(28)) # Perfect number
122-
123-
# Get factors for 28 (using explicit tuple handling)
124-
factor_result = aliquot_sum(28, True)
125-
# Since we requested factors, result is tuple (sum, factors)
126-
if isinstance(factor_result, tuple):
127-
print("Factors of 28:", factor_result[1])
128-
128+
print("Factors of 28:", aliquot_sum(28, return_factors=True)[1])
129129
print("Classification of 28:", classify_number(28))
130-
130+
131131
# Large number performance test with targeted exception handling
132132
try:
133133
print("\nCalculating aliquot sum for 10^9...")

0 commit comments

Comments
 (0)