Consider the quadratic:
n^2 + an + b.
Consider integer coefficients a and b in the range:
|a|< 1000, |b| ≤ 1000.
For what values of a and b will the expression produce the maximum number of primes for consecutive values of n, starting with n=0? (Project Euler problem 27)
Since f(0) must be prime, b must be prime and therefore positive.
note that since n=0 is included, the number of consecutive primes generated is n+1
n a b f(n)
1 -996 997 2
2 -499 997 3
3 -325 977 11
4 -245 977 13
5 -197 983 23
6 -163 983 41
7 -131 941 73
8 -121 947 43
10 -105 967 17
70 -61 971 1601 <-- solution
(a,b) = (-61, 971) 71 consecutive primes
When the limit is raised from 1000 to 2000, a consecutive run of 80 primes exists.
n a b f(n)
79 -79 1601 1601
----------------
big = 1000
primes = [n for n in range(100000) if isprime(n)]
primesUnder1000 = [n for n in range(big) if isprime(n)]
best = [0,0,0]
for a in range(-big,big):
for b in primesUnder1000:
temp = [0,a,b,0]
for n in range(200):
f = n**2 + a*n + b
if f in primes:
temp[0] = n
temp[3] = f
else:
if temp[0] > best[0]:
best = temp
print(best)
break
|
Posted by Larry
on 2024-02-08 11:16:46 |