Problem: Consecutive Prime Sum
Some prime numbers can be expressed as a sum of other consecutive prime numbers. For example 5 = 2 + 3, 17 = 2 + 3 + 5 + 7, 41 = 2 + 3 + 5 + 7 + 11 + 13. Your task is to find out how many prime numbers which satisfy this property are present in the range 3 to N subject to a constraint that summation should always start with number 2.
Write code to find out the number of prime numbers that satisfy the above-mentioned property in a given range.
Input Format: First line contains a number N
Output Format: Print the total number of all such prime numbers which are less than or equal to N.
Constraints: 2<N<=12,000,000,000
Solution:
from array import *
num = int(input("num:"))
a = array('i',[2,3,5,7])
p=2
for j in range(p,num):
if j%2 == 0 or j%3==0 or j%5==0 or j%7==0:
continue
else:
a.insert(p+2,j)
p+=1
p=2
count= 0
val = 0
for i in range(p,len(a)):
for j in a:
if val > a[i]:
break
elif val == a[i]:
count += 1
break
else:
val = val+j
j = a[0]
val = 0
print(count)
Comments