-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler027.py
executable file
·38 lines (28 loc) · 932 Bytes
/
euler027.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/python
from primes import genPrimes, _primes
from math import sqrt
import sys
if sys.version_info[0] == 2:
# get rid of 2.x range that produced list instead of iterator
range = xrange
def isPrime(num):
if num < 2:
return False
for p in genPrimes(maxPrime=sqrt(num)):
if not num % p:
return False
return True
def genPrimeCoefficients(maxAbs_a, maxAbs_b):
for b in genPrimes(maxPrime=maxAbs_b):
minA = max(1 - maxAbs_a, 2 - b)
for a in range(minA, maxAbs_a):
n = 1 # already know b is prime!
while isPrime(n * (n + a) + b):
n += 1
yield (n, (a, b), a * b)
def euler27(maxAbs_a=1000, maxAbs_b=1000):
maxCoefs = max(genPrimeCoefficients(maxAbs_a, maxAbs_b), key = lambda x: x[0])
print('With (a,b) = %s, n^2 + an + b is prime for n in [0,%d]. a * b = %d'
% (str(maxCoefs[1]), maxCoefs[0], maxCoefs[2]))
if __name__ == "__main__":
euler27()