Euler Solution 23

From ProgSoc Wiki

Jump to: navigation, search

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

Solutions to Problem 23

Python by Althalus

Runtime:171 seconds

from math import sqrt
def getFactors(num):
    list = [1,]
    for i in range(2,int(sqrt(num)+1)):
        if num%i == 0:
            list.append(i)
            #Added this if statement to handle squares.
            #EG, i was getting the factors of 4 = 1,2,2
            #hence 4 was incorrectly being reported as abundant.
            if num/i not in list: list.append(num/i)
    return list

def isAbundant(num):
    if sum(getFactors(num)) > num: return True
    return False

abundants = []
for i in range(1,28124):
    if isAbundant(i): abundants.append(i)

total=0
for i in range(1,28124):
    sum_of_abundants = False
    for n in abundants:
        if i-n <= 0: break
        elif isAbundant(i-n):
            sum_of_abundants = True
            break
    if sum_of_abundants == False: total+=i

print (total)
Personal tools