Skip to content

Instantly share code, notes, and snippets.

@voith
Created November 16, 2019 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save voith/13e28319d427dace1433f588b43ee9e0 to your computer and use it in GitHub Desktop.
Save voith/13e28319d427dace1433f588b43ee9e0 to your computer and use it in GitHub Desktop.
Find the product of the coefficients, a and b, for the quadratic expression (n**2+n+41) that produces the maximum number of primes for consecutive values of n, starting with n=0.
from math import sqrt
MAX = 999999
sieve = [True] * (MAX + 1)
sieve[0], sieve[1] = False, False
for i in range(2, int(sqrt(MAX)) + 1):
if sieve[i] is True:
for j in range(2 * i, MAX, i):
sieve[j] = False
def len_prime_seq(a, b):
eqn = lambda n: n ** 2 + a * n + b
count = 0
for i in range(0, MAX):
val = eqn(i)
if sieve[val] is True:
count += 1
else:
break
return count
max_len = 0
pair = (0, 0)
for a in range(-999, 999 + 1):
for b in range(-1000, 1000 + 1):
len_seq = len_prime_seq(a, b)
if len_seq > max_len:
max_len = len_seq
pair = (a, b)
print(max_len, pair)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment