Consecutive prime sum :Problem 50
The prime 41, can be written as the sum of six consecutive primes:
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?
Problem Source : Euler Project
Python Code
import math
def largestSumofConsecutivePrime():
def checkPrime(numb):
prime = True
for i in range(int(math.sqrt(numb))):
i = i+2
if numb%i == 0:
prime = False
break
if numb == 2:
return True
else:
return prime
primeList = ()
cumiSumList = ()
sumd = 0
count = 0
cumiSumList =cumiSumList+(0,)
maxPrime = 0
maxLength = 0
for prime in range(1000000):
prime = prime + 2
if checkPrime(prime) == True:
primeList = primeList+(prime,)
sumd = sumd + prime
cumiSumList =cumiSumList+(sumd,)
count = count + 1
#print primeList
#print cumiSumList
for i in range(count):
for j in range(i+1):
if cumiSumList[i]-cumiSumList[j] > 1000000:
break
if (cumiSumList[i]-cumiSumList[j]) in primeList:
if i-j > maxLength:
maxLength = i-j
maxPrime = cumiSumList[i] - cumiSumList[j]
print maxLength,maxPrime
largestSumofConsecutivePrime()
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?
Problem Source : Euler Project
Python Code
import math
def largestSumofConsecutivePrime():
def checkPrime(numb):
prime = True
for i in range(int(math.sqrt(numb))):
i = i+2
if numb%i == 0:
prime = False
break
if numb == 2:
return True
else:
return prime
primeList = ()
cumiSumList = ()
sumd = 0
count = 0
cumiSumList =cumiSumList+(0,)
maxPrime = 0
maxLength = 0
for prime in range(1000000):
prime = prime + 2
if checkPrime(prime) == True:
primeList = primeList+(prime,)
sumd = sumd + prime
cumiSumList =cumiSumList+(sumd,)
count = count + 1
#print primeList
#print cumiSumList
for i in range(count):
for j in range(i+1):
if cumiSumList[i]-cumiSumList[j] > 1000000:
break
if (cumiSumList[i]-cumiSumList[j]) in primeList:
if i-j > maxLength:
maxLength = i-j
maxPrime = cumiSumList[i] - cumiSumList[j]
print maxLength,maxPrime
largestSumofConsecutivePrime()