spadewarrior
July 30th, 2008, 01:53 AM
I've been trying this one for hours. I thought it was easy, apparently not. I can't get the correct answer and I can't see what's wrong with the code.
Here's the problem: http://projecteuler.net/index.php?section=problems&id=23
Here's my code:
#Problem 23
#02 August 2002
#
#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
#abun.
#As 12 is the smallest abun number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abun numbers is 24.
#By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abun 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
#abun numbers is less than this limit.
#Find the sum of all the positive integers which cannot be written as the sum of two abun numbers.
import psyco, time, sys
from sets import Set
from math import sqrt
psyco.full()
s= time.time()
def sumDiv(n): # get sum of divisors of n
divisors = [1]
i = 2
while i < n:
if n % i == 0:
divisors.append(i)
i+=1
return sum(divisors)
maximum =20160 # numbers above this can always be written as the sum of two abundant numbers
n = 12
abun = []
sums =[]
print "Getting abundant numbers..."
while n <= maximum * 2:
divs = sumDiv(n)
if divs > n: #if number is abun
d2n = n * 2
if n not in abun:
abun.append(n) # add to list of abun numbers
sys.stdout.write('\r')
sys.stdout.write(str(n))
sys.stdout.flush()
while d2n < maximum:
if d2n not in sums:
sums.append(d2n)
sys.stdout.write('\r')
sys.stdout.write(str(n))
sys.stdout.flush()
d2n += n
if divs == n:
n2 = n*2
if n2 not in abun:
abun.append(n2)
sys.stdout.write('\r')
sys.stdout.write(str(n))
sys.stdout.flush()
n+=1
print
abun.sort()
print "Finding sums of abun numbers..."
i = 0
abunLength = len(abun)
while i < len(abun):
j = i+1
sys.stdout.write('\r')
sys.stdout.write("Calculating ")
sys.stdout.write(str(i))
sys.stdout.write(" of ")
sys.stdout.write(str(abunLength))
sys.stdout.flush()
same = abun[i] * 2
#if same < maximum:
sums.append(same)
while j < len(abun):
adjacents = abun[i] + abun[j]
#if adjacents < maximum:
sums.append(adjacents)
j += 1
i += 1
print # blank line
i = 0
print "Getting rid of duplicates..."
sums = list(set(sums)) # remove dupes
print "Sorting..."
sums.sort() # sort in order again
print "Finding numbers that can't be written as the sum of two abun numbers..."
i = 0
n = 1
notsum = []
total = 0
while n < maximum :
if n not in sums:
total += n
notsum.append(n)
sys.stdout.write('\r')
sys.stdout.write("Running total: ")
sys.stdout.write(str(total))
sys.stdout.flush()
n+=1
print
print total
print sums
print notsum
print "\n"
print "Answer: ", sum(notsum)
print "Time taken: ", time.time() - s
I'd be grateful for ideas.
Thanks.
Here's the problem: http://projecteuler.net/index.php?section=problems&id=23
Here's my code:
#Problem 23
#02 August 2002
#
#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
#abun.
#As 12 is the smallest abun number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abun numbers is 24.
#By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abun 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
#abun numbers is less than this limit.
#Find the sum of all the positive integers which cannot be written as the sum of two abun numbers.
import psyco, time, sys
from sets import Set
from math import sqrt
psyco.full()
s= time.time()
def sumDiv(n): # get sum of divisors of n
divisors = [1]
i = 2
while i < n:
if n % i == 0:
divisors.append(i)
i+=1
return sum(divisors)
maximum =20160 # numbers above this can always be written as the sum of two abundant numbers
n = 12
abun = []
sums =[]
print "Getting abundant numbers..."
while n <= maximum * 2:
divs = sumDiv(n)
if divs > n: #if number is abun
d2n = n * 2
if n not in abun:
abun.append(n) # add to list of abun numbers
sys.stdout.write('\r')
sys.stdout.write(str(n))
sys.stdout.flush()
while d2n < maximum:
if d2n not in sums:
sums.append(d2n)
sys.stdout.write('\r')
sys.stdout.write(str(n))
sys.stdout.flush()
d2n += n
if divs == n:
n2 = n*2
if n2 not in abun:
abun.append(n2)
sys.stdout.write('\r')
sys.stdout.write(str(n))
sys.stdout.flush()
n+=1
abun.sort()
print "Finding sums of abun numbers..."
i = 0
abunLength = len(abun)
while i < len(abun):
j = i+1
sys.stdout.write('\r')
sys.stdout.write("Calculating ")
sys.stdout.write(str(i))
sys.stdout.write(" of ")
sys.stdout.write(str(abunLength))
sys.stdout.flush()
same = abun[i] * 2
#if same < maximum:
sums.append(same)
while j < len(abun):
adjacents = abun[i] + abun[j]
#if adjacents < maximum:
sums.append(adjacents)
j += 1
i += 1
print # blank line
i = 0
print "Getting rid of duplicates..."
sums = list(set(sums)) # remove dupes
print "Sorting..."
sums.sort() # sort in order again
print "Finding numbers that can't be written as the sum of two abun numbers..."
i = 0
n = 1
notsum = []
total = 0
while n < maximum :
if n not in sums:
total += n
notsum.append(n)
sys.stdout.write('\r')
sys.stdout.write("Running total: ")
sys.stdout.write(str(total))
sys.stdout.flush()
n+=1
print total
print sums
print notsum
print "\n"
print "Answer: ", sum(notsum)
print "Time taken: ", time.time() - s
I'd be grateful for ideas.
Thanks.