77fn aliquot_sum (input_num : Int) raises -> Int:
88 """
99 Finds the aliquot sum of an input integer.
10-
10+
1111 The aliquot sum is the sum of all proper divisors of a number (all positive divisors less than the number itself).
1212 This is a simple O(n) implementation that directly follows the definition.
13-
13+
1414 Parameters:
1515 - input_num: A positive integer whose aliquot sum is to be found.
1616 Returns:
1717 - The aliquot sum of input_num. If input_num is 1, returns 0 since there are no natural numbers less than 1.
1818 Raises:
1919 - Error: If input_num is not positive.
20-
20+
2121 Examples:
2222 - The aliquot sum of 12 is 1 + 2 + 3 + 4 + 6 = 16
2323 - The aliquot sum of 6 is 1 + 2 + 3 = 6 (perfect number)
2424 - The aliquot sum of 15 is 1 + 3 + 5 = 9
2525 - The aliquot sum of 19 is 1 (prime numbers only have 1 as a proper divisor)
2626 - The aliquot sum of 1 is 0 (there are no natural numbers less than 1)
27-
27+
2828 ```mojo
2929 from testing import assert_equal, assert_raises
3030 from aliquot_sum import aliquot_sum, optimized_aliquot_sum
@@ -41,22 +41,22 @@ fn aliquot_sum(input_num: Int) raises -> Int:
4141 """
4242 if input_num <= 0 :
4343 raise Error(" Input must be positive" )
44-
44+
4545 var sum = 0
4646 for divisor in range (1 , input_num // 2 + 1 ):
4747 if input_num % divisor == 0 :
4848 sum += divisor
49-
49+
5050 return sum
5151
5252fn optimized_aliquot_sum (input_num : Int) raises -> Int:
5353 """
5454 Optimized implementation of aliquot sum with O(sqrt(n)) time complexity.
55-
55+
5656 This implementation leverages the mathematical property that divisors come in pairs:
5757 if i is a divisor of n, then n/i is also a divisor. By iterating only up to sqrt(n),
5858 we can find all divisor pairs and significantly improve performance.
59-
59+
6060 Parameters:
6161 - input_num: A positive integer whose aliquot sum is to be found.
6262 Returns:
@@ -66,11 +66,11 @@ fn optimized_aliquot_sum(input_num: Int) raises -> Int:
6666 """
6767 if input_num <= 0 :
6868 raise Error(" Input must be positive" )
69-
69+
7070 var sum = 1 # Start with 1 as it's always a divisor for positive numbers
7171 if input_num == 1 :
7272 return 0 # Special case: aliquot sum of 1 is 0
73-
73+
7474 # Find all divisor pairs up to sqrt(input_num)
7575 var i = 2
7676 while i * i <= input_num:
@@ -80,5 +80,5 @@ fn optimized_aliquot_sum(input_num: Int) raises -> Int:
8080 if i != input_num // i:
8181 sum += input_num // i
8282 i += 1
83-
83+
8484 return sum
0 commit comments