I'm asking about Project Euler problem 27 (https://projecteuler.net/problem=27). I have written a piece of code that either doesn't work, or doesn't work fast enough - I'm new to programming and don't fully understand the meaning of the error I'm receiving.
Anyway, the question is asking me to find which integers $a,b$ with $|a|,|b|<1000$ lead to $n^2 + an + b$ producing the biggest collection of consecutive primes starting with $n=0$. First of all, we notice that $b$ must itself be prime in order for the $n=0$ term to be prime and start the chain. I therefore wrote a piece of code that loops b over all possible prime values and then checks each and every integer $-1000 < a < 1000$ and measures the length of the chain of consecutive primes produced. I've included it below:
n=int(input("Set a bound for range of a and b: "))
def is_prime(n):
if n==1:
return False
elif n==2 or n==3:
return True
elif (n % 2 == 0) or (n % 3 == 0):
return False
elif all(n % i != 0 for i in range(2, ceil(n**0.5+1))):
return True
else:
return False
def seive(n):
primes=[]
for i in range(n):
if is_prime(i)==True:
primes.append(i)
for j in primes: #j can't be allowed to be negative since when m=0 (the first term), we must have m**2+k*m+j prime. Therefore j must be chosen from primes
for k in range(-n,n,1):
chain=[]
for m in range(0,n):
while is_prime(m**2+k*m+j) == True and m**2+k*m+j>0:
chain.append(m**2 + k*m + j)
details = [j,k,len(chain)]
return details
print(seive(n))
Could someone please either explain what I've done wrong and give me a hint on how to get it working? Thanks!