Skip to content

Commit 80eee7e

Browse files
Anusha-DeviEAnusha-DeviEdlesnoff
authored
Improve documentation and clarify edge cases for aliquot sum algorithm (#90)
* Improve documentation and clarify edge cases for aliquot sum * Update maths/aliquot_sum.nim Co-authored-by: Dimitri Lesnoff <54949944+dlesnoff@users.noreply.github.com> * Update maths/aliquot_sum.nim Co-authored-by: Dimitri Lesnoff <54949944+dlesnoff@users.noreply.github.com> * Update maths/aliquot_sum.nim Co-authored-by: Dimitri Lesnoff <54949944+dlesnoff@users.noreply.github.com> * Update maths/aliquot_sum.nim Co-authored-by: Dimitri Lesnoff <54949944+dlesnoff@users.noreply.github.com> --------- Co-authored-by: Anusha-DeviE <itzanushadevi@gmail.com> Co-authored-by: Dimitri Lesnoff <54949944+dlesnoff@users.noreply.github.com>
1 parent 2255239 commit 80eee7e

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

maths/aliquot_sum.nim

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
## Aliquot sum
2+
##
23
## In number theory, the aliquot sum s(n) of a positive integer n is the sum of
34
## all proper divisors of n, that is, all divisors of n other than n itself.
5+
##
6+
## For example:
7+
## - s(12) = 1 + 2 + 3 + 4 + 6 = 16
8+
## - s(1) = 0 (since 1 has no proper divisors)
9+
##
10+
## This function determines whether a positive integer is perfect (s(n) = n), abundant (s(n) > n), or deficient (s(n) < n).
11+
##
12+
## Reference:
413
## https://en.wikipedia.org/wiki/Aliquot_sum
514

615
runnableExamples:
@@ -12,9 +21,16 @@ runnableExamples:
1221
echo fmt"The sum of all the proper divisors of {number} is {sum}"
1322

1423
func aliquotSum*(number: Positive): Natural =
15-
## Returns the sum of all the proper divisors of the number
16-
## Example: aliquotSum(12) = 1 + 2 + 3 + 4 + 6 = 16
24+
## Compute the aliquot sum of a positive integer.
25+
##
26+
## Input:
27+
## - number: a positive integer (number > 0)
28+
## Output:
29+
## - The sum of all proper divisors of 'number'
30+
## Time Complexity: O(number)
31+
## Space Complexity: O(1)
1732
result = 0
33+
# A proper divisor p satisfies `number = pq` with q greater than or equal to 2
1834
for divisor in 1 .. (number div 2):
1935
if number mod divisor == 0:
2036
result += divisor

0 commit comments

Comments
 (0)