Consecutive Prime Sum
Some prime
numbers can be expressed as sums 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 the constraint that summation should always start with number 2.
Write code to find out a number of prime numbers
that satisfy the above-mentioned property in a given range.
Input Format:
Each test case contains a number N <= 1000000000
Output Format:
Print the total number of all such prime numbers
which are less than or equal to N.
Code:
import math
def isprime(n):
if (n==2 or n==3):
return 1
if (n==1 or n%2==0 or n%3==0 ):
return 0
for i in
range(5,int(math.sqrt(n)+1),6):
if(n%i==0 or n%(i+2)==0
):
return 0
return 1
n=int(input())
sum=2
i=3
c=0
while(sum<=n):
if(isprime(i)==1):
sum+=i
if(sum<=n and
isprime(sum)==1):
c+=1
i=i+2
print(c)
Screenshots:
Comments
Post a Comment