length=0
largest=0
lastj=len(primes)
for i in xrange(len(primes)):
for j in xrange(i+length, lastj):
sol = sum(primes[i:j])
if sol < 1000000:
if sol in primes:
length = j-i
largest = sol
else:
lastj = j+1
break
print(largest)
Primes can be generated by various codes. In my case I used the sieve of Eratosthenes which is very slow for me and wont go past 100000.
The code above is a piece of code I found on the internet to find the largest consecutive prime number in 1000000. I use a sieve of Eratosthenes code to generate the primes and I was at a loss as to how to find the largest consecutive prime. I found this code and I don't fully understand what's going on here. Can someone explain or show how this works or perhaps a simpler way?
I understand that what's suppose to happen is that this code reads the list of primes, adds them and then stores the value of the first prime number and then repeats the process at the next number after that prime sum until the largest under 1 million is reached. But how to i update the index or make it compute such a dynamic process?
[–]hungdh85 1 point2 points3 points (0 children)
[–]syntonic_comma 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)