Worked on Project Euler Problem #23 last night. I was successful, though my brute force approach was less than speedy. Below is the not so pretty code. First, all abundant numbers below 28123 are found and appended to a list. Next, positive integers between 1 and 28123 that can not be the sum of two abundant numbers are found. These integers are marked as “valid.” The final step is to sum the valid integers and print the result.

I’m certainly curious as to methods that can improve this task.

#!/usr/bin/python

abundant = []

valid = []

def contains(list, value):

try:

idx=list.index(value)

except ValueError:

idx=-1

return idx

for x in range(2,28123):

sum = 1

for n in range(2,x/2+1):

if x % n == 0:

sum += n

if sum > x:

abundant.append(x)

for num in range(1,28123):

bad_number = False

for j in abundant:

if num-j < 0:

continue

if contains(abundant, num-j) > -1:

bad_number = True; break

if not bad_number:

valid.append(num)

print “Valid: “+str(num)

sum = 0

for num in valid:

sum += num

print str(sum)

abundant = []

valid = []

def contains(list, value):

try:

idx=list.index(value)

except ValueError:

idx=-1

return idx

for x in range(2,28123):

sum = 1

for n in range(2,x/2+1):

if x % n == 0:

sum += n

if sum > x:

abundant.append(x)

for num in range(1,28123):

bad_number = False

for j in abundant:

if num-j < 0:

continue

if contains(abundant, num-j) > -1:

bad_number = True; break

if not bad_number:

valid.append(num)

print “Valid: “+str(num)

sum = 0

for num in valid:

sum += num

print str(sum)