Project Euler Problem 23

Python

def sum_of_divisors(n):
    s = 1
    i = 2
    while i * i <= n:
        if n % i == 0:
            if i * i == n:
                s += i
            else:
                s += i + n // i
        i += 1
    return s

def is_abundant(n):
    return sum_of_divisors(n) > n

LIMIT = 28123
abundant_list = list(filter(is_abundant, range(2, LIMIT)))
abundant_set = set()
for idx1 in range(len(abundant_list)):
    if abundant_list[idx1] > (LIMIT // 2):
        break
    for idx2 in range(idx1, len(abundant_list)):
        n = abundant_list[idx1] + abundant_list[idx2]
        if n <= LIMIT:
            abundant_set.add(n)
        else:
            break

print(sum(range(1, LIMIT + 1)) - sum(abundant_set))