Problem Description
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 whose proper divisors are less than the number is called deficient and a number whose proper divisors exceed the number is called abundant.
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.
Analysis
According to Wolfram Mathworld’s discussion on Abundant Numbers, “Every number greater than 20161 can be expressed as a sum of two abundant numbers. ” So our upper bound is 20161 not 28123.
Using our routine from problem 21 to calculate the sum of proper divisors we create a set of abundant numbers on the fly and use Python’s set operations to make the necessary comparisons. Any number that can’t be summed from two abundant numbers in the set are marked as exceptions and totaled.
Solution
Runs < 3 seconds in Python.
def d(n): s = 1 t = n ** .5 for i in xrange(2, int(t)+1): if n % i == 0: s += i + n / i if t == int(t): s -= t return s abn = set() s = 0 for n in range( 1, 20162 ): if d(n) > n: abn.add(n) if not any( (n-a in abn) for a in abn ): s += n print "Answer to PE23 = ", s
Comments
OEIS reference to this sequence A048242.
This is one of those problems that you shouldn’t spend too much time on as once you have the answer the problems done; there’s no more discovery.





Discussion
No comments for “Project Euler Problem 23 Solution”