11from __future__ import annotations # Enable modern type hints
22from typing import overload
33
4+
45def aliquot_sum (
56 input_num : int , return_factors : bool = False
67) -> int | tuple [int , list [int ]]:
78 """
89 Calculates the aliquot sum of a positive integer. The aliquot sum is defined as
910 the sum of all proper divisors of a number (all divisors except the number itself).
10-
11+
1112 This implementation uses an optimized O(sqrt(n)) algorithm for efficiency.
12-
13+
1314 Args:
1415 input_num: Positive integer to calculate aliquot sum for
1516 return_factors: If True, returns tuple (aliquot_sum, sorted_factor_list)
16-
17+
1718 Returns:
1819 Aliquot sum if return_factors=False
1920 Tuple (aliquot_sum, sorted_factor_list) if return_factors=True
20-
21+
2122 Raises:
2223 TypeError: If input is not an integer
2324 ValueError: If input is not positive
24-
25+
2526 Examples:
2627 >>> aliquot_sum(15)
2728 9
@@ -33,42 +34,42 @@ def aliquot_sum(
3334 # Validate input type - must be integer
3435 if not isinstance (input_num , int ):
3536 raise TypeError ("Input must be an integer" )
36-
37+
3738 # Validate input value - must be positive
3839 if input_num <= 0 :
3940 raise ValueError ("Input must be positive integer" )
40-
41+
4142 # Special case: 1 has no proper divisors
4243 if input_num == 1 :
4344 # Return empty factor list if requested
4445 return (0 , []) if return_factors else 0
45-
46+
4647 # Initialize factors list with 1 (always a divisor)
47- factors = [1 ]
48+ factors = [1 ]
4849 total = 1 # Start sum with 1
49-
50+
5051 # Calculate square root as optimization boundary
5152 sqrt_num = int (input_num ** 0.5 )
52-
53+
5354 # Iterate potential divisors from 2 to square root
5455 for divisor in range (2 , sqrt_num + 1 ):
5556 # Check if divisor is a factor
5657 if input_num % divisor == 0 :
5758 # Add divisor to factors list
5859 factors .append (divisor )
5960 total += divisor
60-
61+
6162 # Calculate complement (pair factor)
6263 complement = input_num // divisor
63-
64+
6465 # Avoid duplicate for perfect squares
65- if complement != divisor :
66+ if complement != divisor :
6667 factors .append (complement )
6768 total += complement
68-
69+
6970 # Sort factors for consistent output
7071 factors .sort ()
71-
72+
7273 # Return based on return_factors flag
7374 return (total , factors ) if return_factors else total
7475
@@ -79,16 +80,16 @@ def classify_number(n: int) -> str:
7980 - Perfect: aliquot sum = number
8081 - Abundant: aliquot sum > number
8182 - Deficient: aliquot sum < number
82-
83+
8384 Args:
8485 n: Positive integer to classify
85-
86+
8687 Returns:
8788 Classification string ("Perfect", "Abundant", or "Deficient")
88-
89+
8990 Raises:
9091 ValueError: If input is not positive
91-
92+
9293 Examples:
9394 >>> classify_number(6)
9495 'Perfect'
@@ -100,14 +101,14 @@ def classify_number(n: int) -> str:
100101 # Validate input
101102 if n <= 0 :
102103 raise ValueError ("Input must be positive integer" )
103-
104+
104105 # Special case: 1 is always deficient
105106 if n == 1 :
106107 return "Deficient"
107-
108+
108109 # Calculate aliquot sum (using only the integer version)
109110 s = aliquot_sum (n ) # Default returns only integer
110-
111+
111112 # Determine classification
112113 if s == n :
113114 return "Perfect"
@@ -116,22 +117,22 @@ def classify_number(n: int) -> str:
116117
117118if __name__ == "__main__" :
118119 import doctest
119-
120+
120121 # Run embedded doctests for verification
121122 doctest .testmod ()
122-
123+
123124 # Additional demonstration examples
124125 print ("Aliquot sum of 28:" , aliquot_sum (28 )) # Perfect number
125-
126+
126127 # Get factors for 28 with type-safe access
127128 factor_result = aliquot_sum (28 , return_factors = True )
128129 # Since we know return_factors=True returns a tuple, we can safely unpack
129130 if isinstance (factor_result , tuple ):
130131 _ , factors = factor_result
131132 print ("Factors of 28:" , factors )
132-
133+
133134 print ("Classification of 28:" , classify_number (28 ))
134-
135+
135136 # Large number performance test with targeted exception handling
136137 try :
137138 print ("\n Calculating aliquot sum for 10^9..." )
0 commit comments