from lib_misc import sum_proper_divisors


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


def is_sum_of_two_abundant(n, abundant_numbers, abundant_numbers_set):
    for a in abundant_numbers:
        if a > n:
            return False
        d = n - a
        if d in abundant_numbers_set:
            return True
    return False


def euler_023():
    abundant_numbers = [n for n in range(1, 28123 + 1) if is_abundant(n)]
    abundant_numbers_set = set(abundant_numbers)
    return sum([n for n in range(1, 28123 + 1)
                if not is_sum_of_two_abundant(n, abundant_numbers,
                                              abundant_numbers_set)])


if __name__ == "__main__":
    print("e023.py: {}".format(euler_023()))
    assert(euler_023() == 4179871)
