In [5]:
def is_prime(number):
    if number > 1:
        if number == 2:
            return True
        if number % 2 == 0:
            return False
        for current in range(3, int(math.sqrt(number) + 1), 2):
            if number % current == 0: 
                return False
        return True
    return False

def next_prime(n):
    while True:
        if is_prime(n):
            yield n
        n +=1
In [2]:
import math
In [4]:
print is_prime(15)
False
In [6]:
"""
A primal quadratic equation has coefficients a, b such that

where for a range of n,,

n**2 + a*n + b == p

and p is prime.

since it must be true for n==0, it follows that b must be prime.

So we can start by looking at the positive primes between 0 and 1000 for b.

for each of those, consider all values -1000 to 1000 for a, and see how high n gets before we 
reach a non-prime.

since it must work for n==1, we also know that a+b is prime. since b is prime, 

"""
Out[6]:
'\nA primal quadratic equation has coefficients a, b such that\n\nwhere for a range of n,,\n\nn**2 + a*n + b == p\n\nand p is prime.\n\nsince it must be true for n==0, it follows that b must be prime.\n\nSo we can start by looking at the positive primes between 0 and 1000 for b.\n\nfor each of those, consider all values -1000 to 1000 for a, and see how high n gets before we \nreach a non-prime.\n\nsince it must work for n==1, we also know that a+b is prime. since b is prime, \n\n'
In [16]:
def count_primes(a,b):
    # return number of consecutive primes for n**2 + a*n + b
    primal = True
    n = 0
    while primal:
        p = n**2 + a*n + b
        if not is_prime(p):
            primal = False
            return n
        n += 1
In [17]:
print count_primes(1,41)
40
In [19]:
print count_primes(-79,1601)
80
In [23]:
possible_bs = [i for i in range(0,1001) if is_prime(i)]
print possible_bs[:10]
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
In [25]:
def search_quads():
    maxsofar = [0,0,0]
    for b in possible_bs:
        for a in range(1001):
            checkpos = [a,b,count_primes(a, b)]
            checkneg = [-a,b,count_primes(-a, b)]
            maxsofar = max(maxsofar,checkpos, checkneg,key = lambda x: x[-1])
    return maxsofar
            
In [26]:
print search_quads()
[-61, 971, 71]
In [27]:
print -61*971
-59231
In [ ]: