-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler27.py
41 lines (34 loc) · 927 Bytes
/
euler27.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
39
40
41
def isPrime(n):
if n < 2:
return False
elif n == 2:
return True
elif not (n & 1):
return False
i = 3
while i * i <= n:
if n % i == 0:
return False
i = i + 2
return True
maxPrimes = 0
maxProd = 0
maxA = 0
maxB = 0
limits = 1000
l = [x for x in range(1 - limits, limits) if isPrime(abs(x))]
for a in l:
for b in l:
n = 0
primeCount = 0
while isPrime(n * n + a * n + b):
n = n + 1
primeCount = primeCount + 1
if primeCount > maxPrimes:
maxPrimes = primeCount
maxProd = a * b
maxA = a
maxB = b
print("n^2 {0} {1}n {2} {3} produces primes for {4} consecutive values of n".format(
"-" if maxA < 0 else "+", abs(maxA), "-" if maxB < 0 else "+", abs(maxB), maxPrimes))
print("The product of {0} and {1} is {2}".format(maxA, maxB, maxProd))