Primes sum | Practice | GeeksforGeeks

Given a number N. Find if it can be expressed as sum of two prime numbers.

Example 1:
Input: N = 34
Output: "Yes" 
Explanation: 34 can be expressed as 
sum of two prime numbers.

Example 2:
Input: N = 23
Output: "No"
Explanation: 23 cannnot be expressed as
sum of two prime numbers. 


Your Task:  
You dont need to read input or print anything. Complete the function isSumOfTwo() which takes N as input parameter and returns "Yes" if can be expressed as sum of two prime numbers. else return "No".

Expected Time Complexity: O(n*sqrt(n))
Expected Auxiliary Space: O(1)

Constraints:
1<= N <=104
0
60mqcs20shivamyadav2 weeks ago
#Python solution
def prime(k):           j = 2           while (j*j <=k):               if k % j==0:                   return 0               j = j+1           return 1       for i in range(1,N):           num = N - i           if prime(num) == 1 and prime(i)==1:               return 'Yes'       return 'No'
0
151ashutosh20001 month ago
bool isprime(int n){     for(int i=2;i*i<=n;i++){         if(n%i==0)           return false;     }     return true; }   string isSumOfTwo(int n){   if(n%2){       if(isprime(n-2))           return "Yes";       else            return "No";   }   if(n==4)       return "Yes";      for(int i=3;i<=n/2;i+=2){       if(isprime(i)){           if(isprime(n-i)){               return "Yes";           }       }   }   return "No";   }
+1
akashdsingh0062 months ago
Java Code
 
class Solution {   public static boolean isPrime(int n)   {      if(n<=1)      {          return false;      }      for(int i=2;i<=Math.sqrt(n);i++)      {          if(n%i==0)          {              return false;          }      }      return true;  }   static String isSumOfTwo(int N){       // code here       String y="Yes";       String n="No";       for(int i=2;i<N-1;i++)       {           if(isPrime(i) && isPrime(N-i))               return y;       }       return n;   }}
0
codewithshoaib192 months ago
 static String isSumOfTwo(int N){              // TLE error
       // code here       // int x=-1;       // for(int i=2;i<N;i++){       //     if(prime(i)){       //         x=i;       //     }       //     for(int j=i;j<N;j++){       //         if(prime(j)){       //             if(x+j==N){       //                 return "Yes";       //             }       //         }       //     }       // }       // return "No";              // no error       ArrayList<Integer> list = new ArrayList<>();       for (int i = 2; i <= N; i++) {           if (prime(i)) {               list.add(i);           }       }       // System.out.println(list);       for (int i = 0; i < list.size(); i++) {           int x = list.get(i);           for (int j = 0; j < list.size(); j++) {               if ((x + list.get(j)) > N) {                   break;               }               int xx = list.get(j);               if (x + xx == N) {                   return "Yes";               }           }
       }       return "No";   }   public static boolean prime(int n){       if(n<2){           return false;       }       for(int i=2;i*i<=n;i++){           if(n%i==0){               return false;           }       }       return true;   }
0
sachinupreti1902 months ago
class Solution { public:   string isSumOfTwo(int N){       // code here       // first find all the prime numbers form 1 to n ,       // Then apply 2 pointer algorithm       int i,j;       string p,q;       p="Yes";       q="No";       if(N<4)       return q;       if(N==4)       return p;       int arr[N]={};       for(i=2;i<=N;i++)       {           if(arr[i]==0)           {               for(j=i*i;j<=N;j+=i)               {                   arr[j]=1;               }           }       }                                vector<int>v;       for(i=2;i<=N;i++)       {           if(arr[i]==0)           v.push_back(i);       }       // now apply  2pointer alogithm.              int l=0,r=v.size()-1,c=0;       while(l<r)       {           if(v[l]+v[r] == N)           {               c++;               break;           }           if (v[l]+v[r] < N)           {               l++;           }           else           r--;       }       string a,b;       a="Yes";       b="No";       if(c>0)       {           return a;       }       else       return b;          }};
 
0
mayank20212 months ago
C++bool isprime(int N)   {              for(int i=2; i<=sqrt(N); i++)         {             if (N%i==0)              return 0;                      }              return 1;          }    string isSumOfTwo(int N){              if(N<=2)        return "No";       else        {            for(int i=2; i<=N/2; i++)             {                 if(isprime(i) && isprime(N-i))                  return "Yes";                              }                          return "No";                    }          }
0
ayazmrz983 months ago
static boolean isprime(int n)
   {
       if(n<=1)
       {
           return false;
       }
       for(int i=2;i<=Math.sqrt(n);i++)
       {
           if(n%i==0)
           {
               return false;
           }
       }
       return true;
   }
   static String isSumOfTwo(int N){
       String result="No";
       for(int i=2;i<=N-1;i++)
       {
           if(isprime(i) && isprime(N-i))
           {
               result = "Yes";
               break;
           }
       }
       
       return result;
   }
0
sharmanitish1833 months ago
class Solution {   static String isSumOfTwo(int N){     boolean a[]=prime(N);for(int i=2;i<=N;i++) { if(!a[i])  if(a[N-i]==false) {   return "Yes";  }    }       return "No";    } public static boolean[] prime(int n) {boolean isPrime[]=new boolean[n+1];isPrime[0]=true;isPrime[1]=true;for(int i=2;i<=Math.sqrt(n);i++) { for(int j=2*i;j<=n;j+=i) {  isPrime[j]=true; } }return isPrime;}}
0
saurabhsharma295203 months ago
bool isPrime(int n){
        for(int i = 2;i*i<=n;i++) if(n%i == 0) return 0;
        return 1;
    }
    string isSumOfTwo(int N){
        if(N>3 && (N%2 == 0 || isPrime(N - 2)) ) return "Yes";
        return "No";
    }
0
jayesh293 months ago
Simple Java Solution 
class Solution {
    static String isSumOfTwo(int N){
        if(N<4){
            return "No";
        }
        int rp=N/2;
        for(int i=rp;i>=2;i--){
            if(isPrime(i)){
                int temp = N-i;
                if(isPrime(temp)){
                    return "Yes";
                }
            }   
        }
        return "No";
    }
    static boolean isPrime(int n){
        if(n<=1){
            return false;
        }
        if(n==2 || n==3){
            return true;
        }
        if(n%2==0 || n%3==0){
            return false;
        }
        for(int i=5;(i*i)<=n;i++){
            if(n%i==0 ||(n%(i+2)==0)){
                return false;
            }
        }
        return true;
    }
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.