Project Euler – Problem # 23 – Solved with Python

I first approached this problem with lists…. The test programs were running 15 + minutes , so I tried dictionaries, this gave too much data representation. So I settled with python sets. You can add to a set to build it, they are unique, (don’t have to worry about duplicates), and best of all they can be compared to one another. It worked really smoothly with sets. Note: program still takes about 55 seconds to run.


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.

One Possible Solution:

import time

def factor(number):
    """Factor a number"""
    L = []
    for i in range(1, ((number / 2) + 1)):
        if number % i == 0:
    return L

def sum_factors(x):
    """Sum factors"""
    return sum(x)

def find_abundant_Set():
    """Find abundant Set"""
    abundant_Set = set()
    for m in range(0, 28123):
        x = factor(m)
        y = sum_factors(x)
        if m < y:
    return set(sorted(abundant_Set))

def find_abundant_sums(xy):
    """Find abundant sums"""
    abundant_Set_a = set()
    for x in xy:
        for y in xy:
            zzz = x + y
            if zzz < 28123:
    return set(sorted(abundant_Set_a))

def set_of_all_integers():
    """Set if all integers"""
    all_integers = set()
    for x in range(0, 28123):
    return set(sorted(all_integers))
def find_missing_abundant_sums(yz, zz):
    """Find missing abundant sums"""
    all_positive_int = set()
    all_positive_int = zz - yz
    return set(all_positive_int)

def main():
    """Main program"""
    start_time = time.clock()
    xy = find_abundant_Set()
    yz = find_abundant_sums(xy)
    zz = set_of_all_integers()
    xz = find_missing_abundant_sums(yz, zz)
    tally = sum(xz)
    print "Tally = ", tally
    run_time = time.clock() - start_time
    print "Run time = ", run_time
if __name__ == '__main__':

2 Replies to “Project Euler – Problem # 23 – Solved with Python”

  1. I stumbled on sets as well for this problem, they did make it a lot easier. Here’s my code:

    import sets
    from sets import *

    def isabund(x):
    for i in range(1,x):
    if x%i==0:
    if sum(divs) > x:
    return True
    return False

    abunds = set()

    for j in range(28123):
    if isabund(j):

    abundsums= set()

    for v in abunds:
    for w in abunds:
    if (v+w) <28134:

    nums = range(1,28133)

    for b in nums:
    if b not in abundsums:

    print sum(nonsums)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.