22import doctest
33from typing import overload
44
5+
56@overload
67def aliquot_sum (input_num : int ) -> int : ...
78@overload
89def aliquot_sum (input_num : int , return_factors : bool ) -> tuple [int , list [int ]]: ...
910
11+
1012def aliquot_sum (
1113 input_num : int , return_factors : bool = False
1214) -> int | tuple [int , list [int ]]:
1315 """
14- Calculates the aliquot sum of a positive integer.
16+ Calculates the aliquot sum of a positive integer.
1517 The aliquot sum is the sum of all proper divisors of a number.
16-
18+
1719 Args:
1820 input_num: Positive integer
1921 return_factors: If True, returns (sum, sorted_factor_list)
20-
22+
2123 Returns:
2224 Aliquot sum or (sum, factors) if return_factors=True
23-
25+
2426 Raises:
2527 TypeError: If input not integer
2628 ValueError: If input not positive
27-
29+
2830 Examples:
2931 >>> aliquot_sum(15)
3032 9
@@ -36,16 +38,16 @@ def aliquot_sum(
3638 raise TypeError ("Input must be an integer" )
3739 if input_num <= 0 :
3840 raise ValueError ("Input must be positive integer" )
39-
41+
4042 # Special case: 1 has no proper divisors
4143 if input_num == 1 :
4244 return (0 , []) if return_factors else 0
43-
45+
4446 # Initialize factors and total
4547 factors = [1 ]
4648 total = 1
4749 sqrt_num = int (input_num ** 0.5 )
48-
50+
4951 # Find factors efficiently
5052 for divisor in range (2 , sqrt_num + 1 ):
5153 if input_num % divisor == 0 :
@@ -55,17 +57,18 @@ def aliquot_sum(
5557 if complement != divisor :
5658 factors .append (complement )
5759 total += complement
58-
60+
5961 factors .sort ()
6062 return (total , factors ) if return_factors else total
6163
64+
6265def classify_number (n : int ) -> str :
6366 """
6467 Classifies number based on aliquot sum:
6568 - Perfect: sum = number
6669 - Abundant: sum > number
6770 - Deficient: sum < number
68-
71+
6972 Examples:
7073 >>> classify_number(6)
7174 'Perfect'
@@ -76,24 +79,25 @@ def classify_number(n: int) -> str:
7679 raise ValueError ("Input must be positive integer" )
7780 if n == 1 :
7881 return "Deficient"
79-
82+
8083 s = aliquot_sum (n ) # Always returns int
8184 if s == n :
8285 return "Perfect"
8386 return "Abundant" if s > n else "Deficient"
8487
88+
8589if __name__ == "__main__" :
8690 doctest .testmod ()
87-
91+
8892 print ("Aliquot sum of 28:" , aliquot_sum (28 ))
89-
93+
9094 # Handle tuple return explicitly
9195 result = aliquot_sum (28 , True )
9296 if isinstance (result , tuple ):
9397 print ("Factors of 28:" , result [1 ])
94-
98+
9599 print ("Classification of 28:" , classify_number (28 ))
96-
100+
97101 # Large number test
98102 try :
99103 print ("Aliquot sum for 10^9:" , aliquot_sum (10 ** 9 ))
0 commit comments