Quadratic primes
Problem 27
Euler discovered the remarkable quadratic formula:
It turns out that the formula will produce 40 primes for the consecutive integer values . However, when is divisible by 41, and certainly when is clearly divisible by 41.
The incredible formula was discovered, which produces 80 primes for the consecutive values . The product of the coefficients, −79 and 1601, is −126479.
Considering quadratics of the form:
, where and
where is the modulus/absolute value of
e.g. and
e.g. and
Find the product of the coefficients, and , for the quadratic expression that produces the maximum number of primes for consecutive values of , starting with .
from tools import rwh_primes
def run(limit=300000):
primes = rwh_primes(limit)
prime_set = set(primes)
pos_primes = [i for i in primes if i < 1000]
all_primes = [-i for i in pos_primes] + pos_primes
max_n = 0
max_m = ()
for b in pos_primes:
for a in all_primes:
if a >= b:
continue
if abs(a) not in prime_set:
continue
if (a + b + 1) not in prime_set:
continue
n = 0
while (n * n + a * n + b) in prime_set:
n += 1
if n > max_n:
max_n, max_m = n, (a, b)
return max_m[0] * max_m[1]