forked from nayuki/Project-Euler-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp027.py
More file actions
38 lines (28 loc) · 747 Bytes
/
p027.py
File metadata and controls
38 lines (28 loc) · 747 Bytes
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
#
# Solution to Project Euler problem 27
# Copyright (c) Project Nayuki. All rights reserved.
#
# https://www.nayuki.io/page/project-euler-solutions
# https://github.com/nayuki/Project-Euler-solutions
#
import eulerlib, itertools
def compute():
ans = max(((a, b) for a in range(-999, 1000) for b in range(2, 1000)),
key=count_consecutive_primes)
return str(ans[0] * ans[1])
def count_consecutive_primes(ab):
a, b = ab
for i in itertools.count():
n = i * i + i * a + b
if not is_prime(n):
return i
isprimecache = eulerlib.list_primality(1000)
def is_prime(n):
if n < 0:
return False
elif n < len(isprimecache):
return isprimecache[n]
else:
return eulerlib.is_prime(n)
if __name__ == "__main__":
print(compute())