MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / aliquot_sum

Function aliquot_sum

maths/aliquot_sum.py:1–42  ·  view source on GitHub ↗

Finds the aliquot sum of an input integer, where the aliquot sum of a number n is defined as the sum of all natural numbers less than n that divide n evenly. For example, the aliquot sum of 15 is 1 + 3 + 5 = 9. This is a simple O(n) implementation. @param input_num: a positi

(input_num: int)

Source from the content-addressed store, hash-verified

1def aliquot_sum(input_num: int) -> int:
2 """
3 Finds the aliquot sum of an input integer, where the
4 aliquot sum of a number n is defined as the sum of all
5 natural numbers less than n that divide n evenly. For
6 example, the aliquot sum of 15 is 1 + 3 + 5 = 9. This is
7 a simple O(n) implementation.
8 @param input_num: a positive integer whose aliquot sum is to be found
9 @return: the aliquot sum of input_num, if input_num is positive.
10 Otherwise, raise a ValueError
11 Wikipedia Explanation: https://en.wikipedia.org/wiki/Aliquot_sum
12
13 >>> aliquot_sum(15)
14 9
15 >>> aliquot_sum(6)
16 6
17 >>> aliquot_sum(-1)
18 Traceback (most recent call last):
19 ...
20 ValueError: Input must be positive
21 >>> aliquot_sum(0)
22 Traceback (most recent call last):
23 ...
24 ValueError: Input must be positive
25 >>> aliquot_sum(1.6)
26 Traceback (most recent call last):
27 ...
28 ValueError: Input must be an integer
29 >>> aliquot_sum(12)
30 16
31 >>> aliquot_sum(1)
32 0
33 >>> aliquot_sum(19)
34 1
35 """
36 if not isinstance(input_num, int):
37 raise ValueError("Input must be an integer")
38 if input_num <= 0:
39 raise ValueError("Input must be positive")
40 return sum(
41 divisor for divisor in range(1, input_num // 2 + 1) if input_num % divisor == 0
42 )
43
44
45if __name__ == "__main__":

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected