whackedspinach
April 8th, 2010, 02:38 AM
I'm trying to solve Project Euler problem #23 (http://projecteuler.net/index.php?section=problems&id=23). I think the code works, but it is terribly inefficient (I killed it after 10 minutes of number crunching). What is a better way to solve this problem?
'''Project Euler - Problem 23
"Find the sum of all the positive integers which cannot be
written as the sum of two abundant numbers."'''
def sum_divisors(n):
"""Sums the proper divisors of n."""
sum = 1
for x in xrange(2, int(n ** 0.5) + 1):
if (n % x == 0):
sum += x + n / x
if (n ** 0.5) == int(n ** 0.5): sum -= (n ** 0.5)
return sum
def is_abundant(n):
"""Checks if the sum of the divisors of n is greater than n."""
if sum_divisors(n) > n:
return True
else: return False
def find_abundants(limit):
"""Finds all abundant numbers up to the specified limit"""
abundants = [x for x in xrange(1, limit) if is_abundant(x)]
return abundants
abundants_below_28123 = find_abundants(28123)
sums = [x + y for x in abundants_below_28123 for y in abundants_below_28123 if x + y < 28123]
answer = 0
for x in xrange(1, 28123):
if x in sums: pass
else: answer += x
print answer
'''Project Euler - Problem 23
"Find the sum of all the positive integers which cannot be
written as the sum of two abundant numbers."'''
def sum_divisors(n):
"""Sums the proper divisors of n."""
sum = 1
for x in xrange(2, int(n ** 0.5) + 1):
if (n % x == 0):
sum += x + n / x
if (n ** 0.5) == int(n ** 0.5): sum -= (n ** 0.5)
return sum
def is_abundant(n):
"""Checks if the sum of the divisors of n is greater than n."""
if sum_divisors(n) > n:
return True
else: return False
def find_abundants(limit):
"""Finds all abundant numbers up to the specified limit"""
abundants = [x for x in xrange(1, limit) if is_abundant(x)]
return abundants
abundants_below_28123 = find_abundants(28123)
sums = [x + y for x in abundants_below_28123 for y in abundants_below_28123 if x + y < 28123]
answer = 0
for x in xrange(1, 28123):
if x in sums: pass
else: answer += x
print answer