Problem 50

completed December 17, 2011

Code

def isprime(n):
	if n == 0 or n == 1: return 'false'
	for i in range(2, int(n**.5 + 1)):
		if n%i == 0: return 'false'
	return 'true'


#compile primes list
primeslist = []
for p in range(10**6):
	if isprime(p) == 'true': primeslist.append(p)

print len(primeslist)
print sum(primeslist[0:2])

#test all lists of primes
consecutiveprimes = []
i = 0
for j in range(0, len(primeslist), 2):
	print i, j
	thissum = sum(primeslist[i:j])
	if thissum > 10**6: break; break
	if isprime(thissum) == 'true':
		consecutiveprimes.append((j-i, thissum))
print max(consecutiveprimes)
for i in range(1, len(primeslist)-1):
	for j in range(i+1, len(primeslist), 2):
		print i, j
		thissum = sum(primeslist[i:j])
		if thissum > 10**6: 
			print max(consecutiveprimes)
			break; break
		if isprime(thissum) == 'true':
			consecutiveprimes.append((j-i, thissum))