Created
November 16, 2019 19:38
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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