(3 votes, average: 5.00 out of 5)

## Project Euler Problem 23 Solution

#### 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 instead of 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 totaled.

#### Solution

Runs < 3 seconds in Python.

```from math import sqrt   def d(n): s = 1 t = sqrt(n) for i in range(2, int(t)+1): if n % i == 0: s += i + n / i if t == int(t): s -= t return s   limit = 20162 s = 0 abn = set() for n in range(1, limit): 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```

• 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 not much more to discover.

## Discussion

### 5 Responses to “Project Euler Problem 23 Solution”

1. Hi, I’m new on python and I just start the project-euler. I think I learned a lot from your code. It’s really amazing!

Posted by kilimanjaroup | August 17, 2012, 3:55 AM
• Thanks for commenting. Always glad to help when we have time. Good luck with Python – it’s a great language.

Posted by Mike | August 21, 2012, 2:03 PM
2. Can you explain this line in your second function?

if not any( (n-a in abn) for a in abn ):
s += n

and also this line in your first function:
if t == int(t): s -= t

Posted by John | February 2, 2013, 2:04 PM
• Hi John,

The line:

`if t == int(t): s -= t`

is used to remove an extra sqrt(n) that was counted twice for numbers that are perfect squares, such as 4, 9, 16, 25, …

Posted by Mike | February 8, 2013, 4:51 PM
• ```if not any( (n-a in abn) for a in abn ):
s += n```

This is a simple list comprehension that checks for sums of abundant numbers in our growing set. If it is not found, then it’s added to the total.

Posted by Mike | February 8, 2013, 5:03 PM