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

Java Program for Consecutive Prime Sum Problem

Consecutive Prime Sum Problem

TCS CodeVita Season 9 is one of the toughest coding competition in the world. where coders from all around the world fight for the title of world’s best coder. The sample questions which TCS CodeVita has provided this year, has Consecutive Prime Sum as one of its question, this is a good brain storming question, on this page we have provided a Java Code for Consecutive Prime Sum Problem

 

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

Java Code

import java.util.Scanner;
 class Main {
 
    static int prime(int b) {
        int j,cnt;
        cnt=1;
        for (j = 2; j <= b/2; j++) {
            if(b%j==0)
                cnt=0;
        }
        if(cnt==0)
        return 1;
        else
            return 0;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int i,j,n=0,cnt,c=0,sum=0,count=0,k=0;
        Main t = new Main();
        int[] a = new int[25];
        System.out.println("Enter no");
        n = sc.nextInt();
        for (i = 2; i <=n ; i++) {
            cnt=1;
            for (j = 2; j <= n/2; j++) {
                if(i%j==0)
                    cnt=0;
            }
            if(cnt==1) {
                a[k]=i;
                k++;
            }
            
        }
        for (i = 0; i < k; i++) {
            sum=sum+a[i];
            c=t.prime(sum);
            if(c==1)
                count++;
        }
        System.out.println(count);
    }

}
Output
43
4

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++

Python

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

 Python

5 comments on “Java Program for Consecutive Prime Sum (TCS Codevita) | PrepInsta”


  • Srujan Vaddepally

    import java.util.ArrayList;

    class rough{
    public static void main(String[] args) {
    ArrayList al=new ArrayList();
    ArrayList ap=new ArrayList();
    int range=100000,sum=0;
    for(int i=2;i<range;i++)
    {
    if(isPrime(i))
    al.add(i);
    }
    for(int i=0;i<al.size();i++)
    {
    sum+=al.get(i);
    if(al.contains(sum))
    ap.add(sum);
    }
    System.out.println(ap);
    }
    public static boolean isPrime(int x)
    {
    int count=0;
    for(int i=1;i<=x/2;i++)
    if(x%i==0)
    count++;
    if(count==1)
    return true;
    return false;
    }
    }


  • Akshit

    I dont find this solution correct!!
    Here is my solution
    import java.util.List;
    import java.util.Scanner;

    public class ConsecutivePrimeSum {

    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println(“Enter N : “);
    int n = sc.nextInt();
    int sum =0; int k =1;
    int count =0;
    int arr[] = new int[25];
    arr[0] = 2;
    for(int i = 3 ; i<=n ;i++){
    int flag = 0;
    for(int j = 2 ; j <= i/2 ; j++) {
    if(i%j == 0)
    flag =1;
    }
    if(flag == 0) {
    arr[k] =i;
    k++;
    }
    }

    for(int i =0 ; i<k;i++) {
    sum = sum +arr[i];
    int res =0;
    if(sum !=2 && sum <= n) {
    res = isPrime(sum);
    if(res == 0) {
    System.out.println("Sum of Consecutive prime no : " + sum);
    count++;
    }
    }

    }


    • Akshit

      a little condition check for n
      import java.util.List;
      import java.util.Scanner;

      public class ConsecutivePrimeSum {

      public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      System.out.println(“Enter N : “);
      int n = sc.nextInt();
      int sum =0; int k =1;
      int count =0;
      int arr[] = new int[25];
      arr[0] = 2;
      for(int i = 3 ; i<=n ;i++){
      int flag = 0;
      for(int j = 2 ; j <= i/2 ; j++) {
      if(i%j == 0)
      flag =1;
      }
      if(flag == 0) {
      arr[k] =i;
      k++;
      }
      }

      for(int i =0 ; i<k;i++) {
      sum = sum +arr[i];
      int res =0;
      if(sum !=2 && sum <= n) {
      res = isPrime(sum);
      if(res == 0) {
      System.out.println("Sum of Consecutive prime no : " + sum);
      count++;
      }
      }

      }


      • 209X1A05G2

        package TCS_prac;

        import java.util.ArrayList;
        import java.util.Scanner;

        public class consecutive_prime_sum
        {
        public static void main(String[] args)
        {
        System.out.println(“Enter the number : “);
        Scanner sc=new Scanner(System.in);
        int n= sc.nextInt();
        ArrayList al=new ArrayList();
        for(int i=3;i<=n;i++)
        {
        if(ifPrime(i))
        al.add(i);
        }

        System.out.println(al);

        int sum =2,c=0;
        for(int i : al)
        {
        sum +=i;
        if(al.contains(sum))
        {
        System.out.println(sum);
        c++;
        }
        }
        System.out.println(c);

        }
        static boolean ifPrime(int n)
        {
        int c=0;
        for (int i=1;i<=n;i++)
        {
        if(n%i==0)
        {
        c++;

        }

        }
        if(c==2)
        return true;
        return false;
        }
        }
        This was the exact solution !!