TechoSagar: Pairs of prime number

Search

Google Alert - jobs

Pairs of prime number

Print all pairs(sets) of prime numbers (p,q) such that p*q <= n, where n is given number.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N.

Output:
Print the all pairs of prime numbers in increasing order and in single line.
Example:
15 :- 2 2 2 3 2 5 2 7 3 2 3 3 3 5 5 2 5 3 7 2
42 :-  2 2 2 3 2 5 2 7 2 11 2 13 2 17 2 19 3 2 3 3 3 5 3 7 3 11 3 13 5 2 5 3 5 5 5 7 7 2 7 3 7 5 11 2 11 3 13 2 13 3 17 2 19 2


Constraints:
1 ≤ T ≤ 50
4 ≤ N ≤ 400

Example:
Input
2
4
8
Output
2 2
2 2 2 3 3 2
Your Code
#include<iostream>

using namespace std;

int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int a[250];
int i,j, k=0;
for(i=2; i<=n/2 ; i++)
{  int temp=1;
 //cout<<i<<endl;
for(j=2; j<=i/2; j++)
{   //cout<<i<<endl;
if(i%j==0)
{
 temp=0;
 break;
}
}
if(temp==1)
{  //cout<<i;
a[k]=i;
// cout<<a[k]<<endl;
k++; 
}
  }
  
  
  for(i=0; i<k; i++)
  {
  for(j=0; j<k; j++)
  {
  if(a[i]*a[j]<=n)
  {
  cout<<a[i]<<" "<<a[j]<<" ";
  }
  }
  } 
      cout<<endl;
}
}

Follow us

Follow Bijendra Kumar on Facebook