[root@www ~]# python twentyseven.py
(-61, 971) -59231
Started at: Sat Oct 4 21:08:48 2008 Ended at: Sat Oct 4 21:08:51 2008
[root@www ~]# cat twentyseven.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
>>> possibleB[len(possibleB)-1:len(possibleB)]
[997]
>>> possibleAB[0]
(-1, 2)
>>> min([ fBool(s,0) for s in possibleAB])
True
>>> min([ fBool(s,1) for s in possibleAB])
True
"""
import time
starttime = time.asctime()
import numbthy
# f(n) = n*n +a*n + b
# when n = 0, b must be prime number < 1000
# when n = 1, a = f(n) - b - 1
possibleB = filter(numbthy.isprime, range(1,1000))
primesLT2000 = filter(numbthy.isprime, range(1,2000))
def possibleA(b):
"""
Values of a for which f(a,b,1) is prime
>>> possibleA(0)[0]
1
"""
return filter( lambda y: abs(y) < 1000 ,[ ((-b - 1) + x) for x in primesLT2000])
possibleAB = []
for b in possibleB:
tempList = possibleA(b)
for a in tempList:
possibleAB.append((a,b))
def f(a,b,n):
return n*n + a*n + b
def fBool(ab,n):
result = f(ab[0],ab[1], n)
if result > 0:
return numbthy.isprime(result)
else:
return False
def fPrimeRun(ab):
"""
>>> fPrimeRun((0,2))
1
"""
n = 0
while fBool(ab, n + 1):
n += 1
return n
longestRun = max([fPrimeRun(ab) for ab in possibleAB])
for ab in possibleAB:
if fPrimeRun(ab) == 70:
print ab, ab[0] * ab[1]
print "Started at: ", starttime, " Ended at: ",time.asctime()
if __name__ == "__main__":
import doctest
doctest.testmod()
Saturday, October 4, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment