Non-abundant sums

A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.

A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.

As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.

Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.


Idea

Given an upper bound: 28123,we could

  • calculate all abundant numbers
  • make 2-combination of all abundant numbers(with replacement)
  • sum of each 2-combination and remove duplicate
  • sum of all 2-combination, as all the positive integers which can be written as the sum of two abundant numbers
  • subtract this sum from sum of 1 to 28123

In [1]:
from itertools import combinations_with_replacement
In [2]:
def get_divisors(n):
    divisors = [1]
    d = 2
    while pow(d, 2) < n:
        if n % d == 0:
            divisors.extend([d, n // d])
        d += 1
    if pow(d, 2) == n:
        divisors.append(d)
    return divisors
In [3]:
get_divisors(1)
Out[3]:
[1]
In [4]:
get_divisors(12)
Out[4]:
[1, 2, 6, 3, 4]
In [5]:
def get_abundant_numbers():
    n = 1
    while n <= 28123:
        if sum(get_divisors(n)) > n:
            yield n
        n += 1
In [6]:
all_abundant_numbers = list(get_abundant_numbers())
In [7]:
print(all_abundant_numbers[:10])
print(all_abundant_numbers[-10:])
[12, 18, 20, 24, 30, 36, 40, 42, 48, 54]
[28086, 28092, 28098, 28100, 28104, 28110, 28112, 28116, 28120, 28122]
In [8]:
len(all_abundant_numbers)
Out[8]:
6965
In [9]:
def solve():
    combi_abundant_number = combinations_with_replacement(all_abundant_numbers, 2)
    sum_of_two_abundant_number = set(map(lambda c: c[0] + c[1], combi_abundant_number))
    sum_of_two_abundant_number_le28123 = filter(lambda s: s <= 28123, sum_of_two_abundant_number)
    return sum(range(28124)) - sum(sum_of_two_abundant_number_le28123)
In [10]:
solve()
Out[10]:
4179871