The Algorithms logo
The Algorithms
À proposFaire un don

Aliquot Sum

i
T
d
d
p
A
H
def aliquot_sum(input_num: int) -> int:
    """
    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 positive integer whose aliquot sum is to be found
    @return: the aliquot sum of input_num, if input_num is positive.
    Otherwise, raise a ValueError
    Wikipedia Explanation: https://en.wikipedia.org/wiki/Aliquot_sum

    >>> aliquot_sum(15)
    9
    >>> aliquot_sum(6)
    6
    >>> aliquot_sum(-1)
    Traceback (most recent call last):
      ...
    ValueError: Input must be positive
    >>> aliquot_sum(0)
    Traceback (most recent call last):
      ...
    ValueError: Input must be positive
    >>> aliquot_sum(1.6)
    Traceback (most recent call last):
      ...
    ValueError: Input must be an integer
    >>> aliquot_sum(12)
    16
    >>> aliquot_sum(1)
    0
    >>> aliquot_sum(19)
    1
    """
    if not isinstance(input_num, int):
        raise ValueError("Input must be an integer")
    if input_num <= 0:
        raise ValueError("Input must be positive")
    return sum(
        divisor for divisor in range(1, input_num // 2 + 1) if input_num % divisor == 0
    )


if __name__ == "__main__":
    import doctest

    doctest.testmod()
À propos de cet Algorithme

The aliquot sum $s(n)$ of a positive integer $n$ is the sum of all proper divisors of $n$, that is, all divisors of $n$ other than the number $n$ itself. That is:

$$ s(n) = \sum_{d | n, d \neq n} {d} $$

So, for example, the aliquot sum of the number $15$ is $(1 + 3 + 5) = 9$

Aliquot sum is a very useful property in Number Theory, and can be used for defining:

  • Prime Numbers
  • Deficient Numbers
  • Abundant Numbers
  • Perfect Numbers
  • Amicable Numbers
  • Untouchable Numbers
  • Aliquot Sequence of a number
  • Quasiperfect & Almost Perfect Numbers
  • Sociable Numbers

Facts about Aliquot Sum

  • 1 is the only number whose aliquot sum is 0
  • The aliquot sums of perfect numbers is equal to the numbers itself
  • For a semiprime number of the form $pq$, the aliquot sum is $p + q + 1$
  • The Aliquot sum function was one of favorite topics of investigation for the world famous Mathematician, Paul Erdős

Approach on finding the Aliquot sum

Step 1: Obtain the proper divisors of the number

We loop through all the numbers from $1$ to $[\frac{n} 2]$ and check if they divide $n$, which if they do we add them as a proper divisor.

The reason we take the upper bound as $[\frac{n} 2]$ is that, the largest possible proper divisor of an even number is $\frac{n} 2 $, and if the number is odd, then its largest proper divisor is less than $[\frac{n} 2]$, hence making it a foolproof upper bound which is computationally less intensive than looping from $1$ to $n$.

Step 2: Add the proper divisors of the number

The sum which we obtain is the aliquot sum of the number

Implementations

Sources