Pairs of prime number | Practice | GeeksforGeeks

Find all pairs (sets) of prime numbers (p,q) such that p*q <= n, where n is given number. 
 
Example 1:
Input: n = 4
Output: 2 2
Explanation: Pair (2, 2) which has both prime 
numbers as well as satisfying the condition 
2*2 <= 4.

Example 2:
Input: n = 8
Output: 2 2 2 3 3 2
Explanation: Pairs(2, 2), (2, 3) and (3, 2)
which has both prime numbers and satisfying
the given condition.

 
Your Task:
You don't need to read or print anything. Your task is to complete the function prime_pairs() which takes n as input parameter and returns a list of pairs in ascending order.
 
Expected Time Complexity: O(n2* sqrt(n))
Expected Space Complexity: O(1)
 
Constraints:
1 <= n <= 400
+1
deepakkoshta8061 month ago
class Solution{
	public:
   	vector<int> prime_pairs(int n){
   	    int N=(n/2)+1,i,j;
   	    vector<int> v,realv;
   	    for(i=2;i<=N;i++)
   	    {
   	        for(j=2;j<i;j++)
   	            if(i%j==0)
   	                break;
   	        if(i==j)
   	            v.push_back(i);
   	    }
   	    for(int k=0;k<v.size();k++)
   	    {
   	        for(int l=0;l<v.size();l++)
   	        {
   	            if(v[k]*v[l]<=n)
   	            {
   	                realv.push_back(v[k]);
   	                realv.push_back(v[l]);
   	            }
   	        }
   	    }
   	    return realv;
    }    
};
+1
patelaryan94692 months ago
vector<int> prime_pairs(int n){       vector<int>ans;       int arr[n*n],k=0;       for(int i=2;i<n;i++)       {           int c=0;           for(int j=2;j<=i/2;j++)           {               if(i%j==0)               {                   c++;                   break;;               }                          }           if(c==0)           {               arr[k]=i;               k++;           }       }       for(int i=0;i<k;i++)       {            for(int j=0;j<k;j++)            {                if(arr[i]*arr[j]<=n)                {                    ans.push_back(arr[i]);                    ans.push_back(arr[j]);                }                       }                  }       return ans;       // Code here   }    
0
gupta2411sumit2 months ago
// Easy to understand C++ Solution 
bool isPrime( long m ){    if(m<=1)    {        return false ;    }    else if(m==2 )    {        return true ;    }        else if(m%2==0)    {        return false ;    }          for ( long i = 3 ; i<= sqrt(m) ; i += 2)    {        if(m%i==0)        {            return false ;           }    }          return true ;}   vector<int> prime_pairs(int n){       // Code here              vector<int> v ;                     for( int i = 2 ; i<=n/2; i++ )       {           if( isPrime(i))           {               for( int j = 2 ; j<=n/2; j++ )               {                   if( isPrime(j)  && i*j <= n)                   {                       v.push_back(i) ;                       v.push_back(j) ;                   }               }           }       }              return v ;   }    };
0
deepakkoshta8063 months ago
vector<int> prime_pairs(int n){
   	    int i,j;
   	    vector<int> v,a;
   	    for(i=2;i<=n&&i*2<=n;i++)
   	    {
   	        for(j=2;j<=i/2;j++)
   	            if(i%j==0)
   	                break;
   	        if(j==(i/2)+1)
   	            v.push_back(i);
   	    }
   	    for(i=0;i<v.size();i++)
   	        for(j=0;j<v.size();j++)
   	            if(v[i]*v[j]<=n)
   	            {
   	                a.push_back(v[i]);
   	                a.push_back(v[j]);
   	            }    
        return a;
    }
-2
badgujarsachin837 months ago
class Solution{
	public:
	bool prime(int n){
	    if(n==0|| n==1){
	        return false;
	    }
	    for(int i=2;i<=sqrt(n);i++){
	        if(n%i==0){
	            return false;
	        }
	    }
	    return true;
	}
   	vector<int> prime_pairs(int n){
   	    // Code here
   	    vector<int> v;
   	    for(int i=1;i<=n/2;i++){
   	        for(int j=1;j<=n/2;j++){
   	            if(prime(i)==true && prime(j)==true){
   	                if(i*j<=n){
   	                  v.push_back(i);
   	                  v.push_back(j);
   	                }
   	            }
   	        }
   	    }
   	    return v;
    }    
};
0
Aditya Anerao8 months ago
Aditya Anerao
https://uploads.disquscdn.c...
+1
Sagar Sikchi9 months ago
Sagar Sikchi
Java Solution:Time Taken: 0.5/2.3
class Solution {       public static boolean isPrime(int n) {        if(n == 2) return true;        for(int i = 2; i <= Math.sqrt(n); i++) {            if(n % i == 0) return false;        }        return true;    }    public int[] prime_pairs(int n) {        ArrayList<integer> ans = new ArrayList<integer>();        for(int i = 2; i <= n / 2; i++) {            if(isPrime(i)) {                for(int j = 2; j <= n / 2; j++) {                    if(isPrime(j) && i * j <= n) {                        ans.add(i);                        ans.add(j);                    }                }            }        }        return ans.stream().mapToInt(i -> i).toArray();    }}
0
Panasa Teja10 months ago
Panasa Teja
Executed in 0.01secclass Solution{public:   vector<int> prime_pairs(int n){       // Code here       vector<int>v(n+1),k,a;       int i,j;       for(i=2;i<=n;i++){           if(v[i])continue;           for(j=2*i;j<=n;j+=i)v[j]=i;       }       for(i=2;i<v.size();i++)if(!v[i])k.push_back(i); for(i="0;i&lt;k.size();i++){" for(j="0;j&lt;k.size();j++){" if(k[i]*k[j]<="n){a.push_back(k[i]);" a.push_back(k[j]);}="" }="" }="" return="" a;="" }="" <="" code="">
0
Panasa Teja
This comment was deleted.
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.