root@www ~]# python twentyeight.py
669171001
Started at: Wed Oct 8 22:40:25 2008 Ended at: Wed Oct 8 22:40:25 2008
[root@www ~]# cat twentyeight.py
# -*- 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
starttime = time.asctime()
def listOddSquare(N):
"""
Returns list of odd squares for the first N odd numbers starting
at 3
>>> sum(listOddSquare(2))
34
>>> sum(listOddSquare(1))
9
"""
x = range(1,(N +1) * 2)
oddSquare = map(lambda x: x * x, x)
oddSquare = filter(lambda x: x % 2 == 1, oddSquare)
oddSquare = oddSquare[1:]
return oddSquare
def listEvenSquarePlusOne(N):
"""
Returns list of square-plus-one for the first N even numbers starting
at 2.
>>> sum(listEvenSquarePlusOne(2))
22
>>> sum(listEvenSquarePlusOne(1))
5
"""
evenSquarePlusOne = filter(lambda x: x % 2 == 0, range(1,(N + 1) * 2 ))
evenSquarePlusOne = map(lambda x: x*x + 1, evenSquarePlusOne)
return evenSquarePlusOne
def listUpperLeft(N):
"""
for returning average of listOddSquare and listEvenSquarePlusOne
>>> upperLeft(2)
28
>>> upperLeft(1)
7
"""
return listOddSquare(N) + listEvenSquarePlusOne(N)
def upperLeft(N):
return sum(listUpperLeft(N))/2
def listLowerRight(N):
"""Returns n-th element of listEvenSquarePlusOne minus n
>>> sum(listLowerRight(2))
16
>>> sum(listLowerRight(1))
3
"""
return [listEvenSquarePlusOne(N)[i] - (2*(i + 1) ) for i in range(N)]
def total(N):
"""Return sum of diagonals except that center is only counted once
>>> total(2)
101
"""
return sum(listEvenSquarePlusOne(N) + listOddSquare(N) + listLowerRight(N)) + upperLeft(N) + 1
print total(500)
print "Started at: ", starttime, " Ended at: ",time.asctime()
if __name__ == "__main__":
import doctest
doctest.testmod()
[root@www ~]#
Wednesday, October 8, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment