-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathproblem27.py
More file actions
40 lines (33 loc) · 819 Bytes
/
problem27.py
File metadata and controls
40 lines (33 loc) · 819 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
39
40
import math
def is_prime(n):
if n < 0:
return False
for j in range(2, math.sqrt(n)+1):
if n%j == 0:
return False
return True
def generate_primes(n):
return [x for x in range(2, n+1) if is_prime(x)]
def f(a, b, n):
return (n**2 + (a*n) + b)
def sequence_of_primes(a, b):
n = 0
t = True
while t:
t = is_prime(f(a, b, n))
n += 1
return n-1
p = generate_primes(1000)
maximum = 0
for i in p:
for j in p:
l = sequence_of_primes(i, j)
if l > maximum:
maximum = l
nums = (i, j)
l = sequence_of_primes(-i, j)
if l > maximum:
maximum = l
nums = (-i, j)
print("n^2 + %dn + %d yields %d primes" % (nums[0], nums[1], maximum))
print("%d" % (nums[0] * nums[1]))