Skip to content

Commit 51ba6ba

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

1 file changed

Lines changed: 31 additions & 27 deletions

File tree

maths/aliquot_sum.py

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
11
from typing import overload, Tuple
22

3+
34
@overload
45
def aliquot_sum(input_num: int, return_factors: bool = False) -> int: ...
56
@overload
6-
def aliquot_sum(input_num: int, return_factors: bool = True) -> Tuple[int, list[int]]: ...
7+
def aliquot_sum(
8+
input_num: int, return_factors: bool = True
9+
) -> Tuple[int, list[int]]: ...
10+
711

812
def aliquot_sum(
913
input_num: int, return_factors: bool = False
1014
) -> int | Tuple[int, list[int]]:
1115
"""
1216
Calculates the aliquot sum of a positive integer. The aliquot sum is defined as
1317
the sum of all proper divisors of a number (all divisors except the number itself).
14-
18+
1519
This implementation uses an optimized O(sqrt(n)) algorithm for efficiency.
16-
20+
1721
Args:
1822
input_num: Positive integer to calculate aliquot sum for
1923
return_factors: If True, returns tuple (aliquot_sum, sorted_factor_list)
20-
24+
2125
Returns:
2226
Aliquot sum if return_factors=False
2327
Tuple (aliquot_sum, sorted_factor_list) if return_factors=True
24-
28+
2529
Raises:
2630
TypeError: If input is not an integer
2731
ValueError: If input is not positive
28-
32+
2933
Examples:
3034
>>> aliquot_sum(15)
3135
9
@@ -37,41 +41,41 @@ def aliquot_sum(
3741
# Validate input type - must be integer
3842
if not isinstance(input_num, int):
3943
raise TypeError("Input must be an integer")
40-
44+
4145
# Validate input value - must be positive
4246
if input_num <= 0:
4347
raise ValueError("Input must be positive integer")
44-
48+
4549
# Special case: 1 has no proper divisors
4650
if input_num == 1:
4751
return (0, []) if return_factors else 0
48-
52+
4953
# Initialize factors list with 1 (always a divisor)
50-
factors = [1]
54+
factors = [1]
5155
total = 1 # Start sum with 1
52-
56+
5357
# Calculate square root as optimization boundary
5458
sqrt_num = int(input_num**0.5)
55-
59+
5660
# Iterate potential divisors from 2 to square root
5761
for divisor in range(2, sqrt_num + 1):
5862
# Check if divisor is a factor
5963
if input_num % divisor == 0:
6064
# Add divisor to factors list
6165
factors.append(divisor)
6266
total += divisor
63-
67+
6468
# Calculate complement (pair factor)
6569
complement = input_num // divisor
66-
70+
6771
# Avoid duplicate for perfect squares
68-
if complement != divisor:
72+
if complement != divisor:
6973
factors.append(complement)
7074
total += complement
71-
75+
7276
# Sort factors for consistent output
7377
factors.sort()
74-
78+
7579
# Return based on return_factors flag
7680
return (total, factors) if return_factors else total
7781

@@ -82,16 +86,16 @@ def classify_number(n: int) -> str:
8286
- Perfect: aliquot sum = number
8387
- Abundant: aliquot sum > number
8488
- Deficient: aliquot sum < number
85-
89+
8690
Args:
8791
n: Positive integer to classify
88-
92+
8993
Returns:
9094
Classification string ("Perfect", "Abundant", or "Deficient")
91-
95+
9296
Raises:
9397
ValueError: If input is not positive
94-
98+
9599
Examples:
96100
>>> classify_number(6)
97101
'Perfect'
@@ -103,14 +107,14 @@ def classify_number(n: int) -> str:
103107
# Validate input
104108
if n <= 0:
105109
raise ValueError("Input must be positive integer")
106-
110+
107111
# Special case: 1 is always deficient
108112
if n == 1:
109113
return "Deficient"
110-
114+
111115
# Calculate aliquot sum (using only the integer version)
112116
s = aliquot_sum(n) # Default returns only integer
113-
117+
114118
# Determine classification
115119
if s == n:
116120
return "Perfect"
@@ -119,15 +123,15 @@ def classify_number(n: int) -> str:
119123

120124
if __name__ == "__main__":
121125
import doctest
122-
126+
123127
# Run embedded doctests for verification
124128
doctest.testmod()
125-
129+
126130
# Additional demonstration examples
127131
print("Aliquot sum of 28:", aliquot_sum(28)) # Perfect number
128132
print("Factors of 28:", aliquot_sum(28, return_factors=True)[1])
129133
print("Classification of 28:", classify_number(28))
130-
134+
131135
# Large number performance test with targeted exception handling
132136
try:
133137
print("\nCalculating aliquot sum for 10^9...")

0 commit comments

Comments
 (0)