Consecutive Prime Number

TCS Codevita Previous year coding question

Question:

Some Prime numbers can be expressed as a sum of other consecutive prime numbers. 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 a code to find the number of prime numbers that satisfy the above mentioned property in a given range.

Constraints:

3 >= N <= 10000

Input Format:

N – First line contain a integer number

Output Format:

Print the total number of all such prime numbers which are less than or equal to N.

Test Cases :

Test Case 1:

Input:
20
Output:
2

Test Case 2:

Input:
1000
Output:
5

Answer :

N = int(input())
prime = []
count = 0
prime = [ i for i in range (2, N + 1) if all (i % j != 0 for j in range (2, int (i ** 0.5) + 1))]
for i in range(2, len(prime)):
    sum = 0
    for j in prime:
	    sum += j
	    if sum == prime[i]:
	    	count += 1
	    	break
	    if sum > prime[i]:
		    break
print(count)


Leave a Reply

Your email address will not be published. Required fields are marked *