Problem 23

completed December 5, 2011

Comments

This is taking a lot of work to fix the bugs in here. This dictionary class is really helping out a lot though. It means I don’t have to go through a whole bunch of numbers a million times just to get an answer. I’ve managed to cut the time back on this significantly, but it still has some work to do. It also give the wrong answer, which isn’t a surprise. But I think it’s on the right track.

Oh great, I found my mistake. Note to self: be causious when changing a variable’s name. Not changing the name everywhere makes the script not work!

Code

from time import time
start_time= time()
print """Process started
----------"""

bound = 28123+1

#Create function to determine if a number is perfect
def perfect(n):
	a = 0
	for i in range(1, n):
		if n%i == 0:
			a = a + i
	if a > n:
		return 1
	elif a < n:
		return -1
	else:
		return 0

#Build list of all abundant numbers
abundantsums = []
abundant = dict()
collected = 'FALSE'
for i in range(bound):
	if perfect(i) < 1: continue
	for j in range(i, bound):
		if collected is 'FALSE':
			abundant[j] = perfect(j)
		if abundant.get(j) < 1: continue
		if i + j > bound:
			collected = 'TRUE'; break
		elif ((i + j) not in abundant):
			print i, j
			abundantsums.append(i + j)

#Find the sum of all numbers not in the previous list
sum = 0
for r in range(bound):
	if r not in abundant:
		sum = sum + r
		
print sum


end_time= time()
elapse_time= end_time - start_time
print '----------'
print elapse_time, 'seconds have passed'