Quadratic Primesproblem 27
Euler discovered the remarkable quadratic formula:
N2 + n + 41
It turns out that the formula would produce the primes for the consecutive values n = 0 to 39. However, when n = 41, 402 + + + = (+ 1) +-is divisible by, and certainly when n = 412 + + clearly divisible by 41.
The incredible formula N2? 1601N + is discovered, which produces primes for the consecutive values n = 0 to 79. The product of the coefficients,?? and 1601, is? 126479.
Considering Quadratics of the form:
N2 +
an +
b, where |
a| < |
B| < 1000
where |
N| is the Modulus/absolute value of
n
e.g. |11| = One and |? 4| = 4
Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum Nu Mber of primes for consecutive values of N, starting with n = 0.
Python code:
Import Mathdef IsPrime (x): If X<3:return False for I in Range (2,int (math.sqrt (x)) +1): If x%i==0: Return False return truedef func (A, B): K=0 while True:if IsPrime (k*k+a*k+b): k+=1 Else:break return k-1maxa,maxb=0,0num=0for J in Range ( -999,1000): If IsPrime (j): For I in RA Nge ( -999,1000): Temp=func (i,j) if Temp>num:num=temp maxa,maxb=i,jp Rint (MAXA*MAXB)
Time:3s
Euler program (python) problem 27