Thursday, September 18, 2008

Project Euler Problem 23 Abundant Numbers

# -*- coding: utf-8 -*-
"""
<+ MODULE_NAME +>
Project Euler Problem Set
<+ DESCRIPTION +>
Solutions in Python to the problems at http://projecteuler.net

Licensed under the Creative Commons license; see http://creativecommons.org for more details. In short, this means you can freely reuse, modify and distribute this program, also commercially, for as long you provide a proper attribution.

Copyright by Jonathan Mark, jonathanmark.com/aristede.com, jmark@aristede.com

>>> print 881 + 1
882

"""
import time

class Abundant:
""" Class for solving Project Euler project 23"""

def __init__(self):
print time.asctime()
self.abundants = filter(self.abundantQ, range(1,20162))
self.abundantSums = [1 for n in range(20162)]


def abundantQ(self,n):
if n < sum(filter(lambda i: n%i == 0, range(1,n/2 + 1))):
return True
else:
return False

a = Abundant()
final = len(a.abundants)
for i in range(final/2 + 1):
for j in range(i,final):
absum = a.abundants[i] + a.abundants[j]
if absum >= 20162:
break
else:
a.abundantSums[absum] = 0

print sum([n*a.abundantSums[n] for n in range(len(a.abundantSums))])
print time.asctime()

if __name__ == "__main__":
import doctest
doctest.testmod()

No comments: