## Project Euler 23: Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.

#### 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. We then add all the numbers that can’t be formed from the sum of two abundant numbers.

#### Project Euler 23 Solution

Runs < 0.530 seconds in Python 2.7.```
from Euler import d # d(n) returns the sum of proper divisors for n
L, s = 20161, 0
abn = set()
for n in range(1, L+1):
if d(n) > n:
abn.add(n)
if not any( (n-a in abn) for a in abn ):
s+= n
print "Project Euler 23 Solution =", s
```

Use this link to get the Project Euler 23 Solution Python 2.7 source.#### Answer

Slowly swipe from either end beginning with the white vertical bar to get an idea of the starting or ending digits. For less drama, just double click the answer area. The distance between the two bars will give you an idea of the magnitude. Touch devices can tap and hold the center of the box between the two bars and choose*define*to reveal the answer.

#### Afterthoughts

- Function
`d`

is listed in Common Functions and Routines for Project Euler - Reference: The On-Line Encyclopedia of Integer Sequences (OEIS) A048242: Numbers that are not the sum of two abundant numbers (not necessarily distinct).
- 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.

*Project Euler 23 Solution last updated*

Hello, maybe 3 years too late, but I hope you can help me find the flaw in my code. I have checked that my versión of the function for summing up proper divisors works alright; so the problems must be the other one, yet I can’t figure out where.

Ok, problem solved.

Glad you got it figured out. I was confused by the lack of an operator in the if statement. Guessing a less than which got filtered out as html start tag op.

I get an error when I try to import from Euler, so I constructed the d list from scratch. This is my code and it gives an answer of 4123438 which is apparently wrong. Obviously I am a beginner in Python. Where have I gone wrong? Thank you.

sm=0

d = []

for x in range(1,20163):

sm=0

for y in range(1,x-1):

if x%y == 0:

sm = sm + y

next

d.append(sm)

# print x, sm

next

#for x in range(1,len(d)):

# print x, d[x]

L, s = 20162, 0

abn = set()

for n in range(1, L):

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

Here’s a solution with the ‘d’ function explicitly defined.

You can find my Python Euler library here:

Project Euler Library

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

Hi John,

The line:

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

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.

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!

Thanks for commenting. Always glad to help when we have time. Good luck with Python – it’s a great language.