Python Program for Consecutive Prime Sum (TCS Codevita) | PrepInsta

Python Program for consecutive prime sum

Consecutive Prime Sum Problem

Consecutive prime sum is one of the most popular challenging questions which was asked in TCS CodeVita Season 9 sample questions. This problems check your logical thinking ability. The difficulty level of this problem is between low-medium, regarding TCS CodeVita Season 9, other sample questions

Problem Description

Question – :  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

S.no Input Output Comment
1 20 2 (Below 20 there are two such members; 5 and 17) 5=2+3 17=2+3+65+7
2 15 1

Python Code

num = int(input())
arr = []
sum = 0
count = 0
if num > 1:
    for i in range(2, num + 2):
        for j in range(2, i):
            if i % j == 0:
                break
        else:
            arr.append(i)
def is_prime(sum):
    for i in range(2, (sum // 2) +2):
        if sum % i == 0:
            return False
        else:
            return True
for i in range(0, len(arr)):
    sum = sum + arr[i]
    if sum <= num:
        if is_prime(sum):
            count = count + 1

print(count)
Output
20
2

Consecutive Prime Sum Problem in Other Coding Languages

C

To find the solution of Consecutive Prime sum problem in C Programming language click on the button below:

C

C++

To find the solution of Consecutive Prime sum problem in C++ Programming language click on the button below:

C++

Java

To find the solution of Consecutive Prime sum problem in Java Programming language click on the button below:

Java

26 comments on “Python Program for Consecutive Prime Sum (TCS Codevita) | PrepInsta”


  • RAMU

    n=int(input())
    primes=[]
    s_primes=[]
    i=3
    count=0
    while i n:
    break
    if s in primes:
    s_primes.append(s)

    print(len(s_primes))


  • Vamshi

    n,p=int(input(‘enter number’)),[]
    for i in range(2,n):
    for j in range(2,i):
    if i%j==0:
    break
    else:
    p.append(i)
    c=0
    def isin(a,b):
    for i in b:
    if a==i:
    return True
    break
    for i in range(2,len(p)):
    s=sum(p[0:i])
    if isin(s,p)==True:
    c+=1
    print(c)


  • Shaswata

    num = int(input())
    prime_number=[]
    mysum = 0
    count=0

    for number in range(1,num + 1):
    if number > 1:
    for i in range(2,number):
    if (number % i) == 0:
    break
    else:
    prime_number.append(number)

    for i in range(len(prime_number)):
    mysum = mysum+prime_number[i]
    if mysum in prime_number:
    count+=1
    print(count-1)


  • Ashish

    Hope Someone Find this Helpful:
    n = int(input(“Enter the Number: “))
    lst = []
    add = 0
    lst1 = []
    if n > 1:
    for i in range(2,n+2):
    for j in range(2,i):
    if i % j == 0:
    break
    else:
    lst.append(i)

    for i in lst:
    add = add + i
    if add < n:
    lst1.append(add)

    inter = set(lst).intersection(set(lst1))
    count = inter-{2}
    print(len(count))