The prime 41, can be written as the sum of six consecutive primes:
41 = 2 + 3 + 5 + 7 + 11 + 13 This is the longest sum of consecutive primes that adds to a prime below one-hundred.
The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.
Which prime, below one-million, can be written as the sum of the most consecutive primes?
I came up with this code, but it only works decently for primes below ten thousand.
Any ideas on how to optimize it?
from pyprimes import *
def sequence_exists(l,ls,limit = 100):
for x in range(0,len(ls)-l):
if x+l > len(ls): return False
if any (ls[i] > limit/6 for i in range(x,x+l,1)) :
return False
test_sum = sum(ls[x:x+l:1])
if (test_sum <limit) and is_prime(test_sum) :
return True
return False
def main():
n = prime_count(10000)
prime_list = list(nprimes(n))
l = 6
for x in range(6,len(prime_list)):
if sequence_exists(x,prime_list,10000):
l=x
print l
if __name__ == '__main__':
main()