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

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


  • Yash

    Yash Gurav Here,
    One of the best solution for the same problem is following :
    #Correct_Solution
    #list_manupulation
    #set_theory

    x=int(input())
    c=0
    b,d=[],[]
    e={2}
    for j in range(2,x+1):
    for i in range(2,j):
    if j%i==0:
    break
    else:
    b.append(j) #creaton of b
    for i in range(0,len(b)):
    c=c+b[i]
    d.append(c) #creation of d
    b_set=set(b)
    d_set=set(d)
    y=b_set.intersection(d_set)
    q=set(y)-set(e)
    print(len(q))


    • Anjali

      Please help me to understand what is wrong with this program
      n=int(input())

      a=[]
      for j in range (n):
      for i in range (2,int(j/2)):
      if (n%i)==0:
      break
      else:
      a.append(i)
      f=[]

      for m in range (len(a)-1):
      s=0
      for n in range (m):
      s=s+a[n]
      if (a[m]==s):
      f.append(a[m])
      else:
      break

      print(f)


  • Pankaj Kumar

    num = int(input())
    list = []
    count=0

    for i in range(2,num+2):
    for j in range(2,i):
    if i%j == 0:
    break
    else:
    list.append(i)
    sum=list[0]
    for j in range(1,len(list)):
    sum=sum+list[j]
    if sum in list:
    count+=1

    print(count)


  • Slayer166

    ashish
    def hcf1(n):
    for i in reversed(range(n)):
    if(n%i==0):
    hcf=i
    break
    return hcf
    n=int(input())
    sum1=2
    count=0
    for i in range(3,n+1):
    if(hcf1(i)==1):
    sum1+=i
    if(hcf1(sum1)==1 and sum1<n+1):
    count+=1
    print(count)


  • Sukesh

    #This is the correct the program

    Input=int(input()); List=[] ; ans=2; count=0; a=0; b=0
    for i in range(2,Input):
    if i ==2 or i==3 or i==5 or i==7: List.append(i)
    else:
    if i%2==1:
    if i%3!=0 and i%5!=0 and i%7!=0: List.append(i)

    if Input>5:
    for i in range(1,len(List)):
    a=List[i]
    ans+=a
    if ans<=List[-1]:
    if ans in List: print(ans);count+=1
    print(count)


  • Sukesh

    Input=int(input())
    List=[] ; ans=0; count=0; a=0; b=0
    for i in range(2,Input):
    if i ==2 or i==3 or i==5 or i==7:List.append(i)
    else:
    if i%2==1:
    if i%3!=0:
    if i%5!=0:
    if i%7!=0:
    List.append(i)
    if Input>5:
    for i in range(0,len(List),2):
    a=List[i]; b=List[i+1]
    ans+=(a+b)
    if ans<=List[-1]:
    if ans in List:
    count+=1
    print(count)


  • Aryan

    N=int(input())
    def isprime(N):
    for i in range(2,N):
    if(N%i==0):
    return(False)
    return(True)

    count=0
    for i in range(3,N):
    sum1=0
    if(isprime(i)==True):
    for j in range(2,i):
    if(isprime(j)==True):
    sum1+=j
    if(sum1==i):
    count+=1

    if(sum1>i):
    break
    print(count)


  • Vascular

    here is the efficient 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):
    prime = [True for i in range(n+1)]
    l=[]
    p = 2
    while (p * p <= n):
    if (prime[p] == True):
    for i in range(p * p, n+1, p):
    prime[i] = False
    p += 1
    if prime[p]:
    return True
    else:
    return False

    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)


  • Archibrata

    import sympy
    # if you haven’t then instal pip sympy
    N = int(input())
    list_of_prime_numbers_within_input_range = [2,]
    output_list_containing_prime_sums = []

    # generating prime number using lambda function
    for x in range(3,n):
    if not any(y for y in range(2,1+int(x**(1/2))) if not x%y):
    list_of_prime_numbers_within_input_range.append(x)

    # or you could simply use sympy.prime(N)… Just an one line code

    for i in range(1,len(list_of_prime_numbers_within_input_range)+1):
    summ = sum(l[:i+1])
    if sympy.isprime(summ)== True and summ<=N:
    output_list_containing_prime_sums.append(summ)

    print(len(output_list_containing_prime_sums))