Amicable numbers

Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numbers.

For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.

Evaluate the sum of all the amicable numbers under 10000.


Idea

A problem of a new math concept.

As instructed, iterate and get divisors of a number, check if it is amicable


In [1]:
def get_divisor(n):
    divisors = [1]
    d = 2
    while pow(d, 2) <= n:
        if n % d == 0:
            divisors.extend([d, n // d])
        d += 1
    return divisors
In [2]:
get_divisor(220)
Out[2]:
[1, 2, 110, 4, 55, 5, 44, 10, 22, 11, 20]
In [3]:
sum(get_divisor(220))
Out[3]:
284
In [4]:
sum(get_divisor(284))
Out[4]:
220
In [5]:
def solve(bound):
    amicable_number = []
    for i in range(1, bound):
        s = sum(get_divisor(i))
        if i == sum(get_divisor(s)) and i != s:
            amicable_number.append(i) 
    return sum(amicable_number)
In [6]:
solve(10000)
Out[6]:
31626