Return two prime numbers

PROBLEM :
Given an even number ( greater than 2 ), return two prime numbers whose sum will be equal to given number. There are several combinations possible. Print only first such pair.

NOTE: A solution will always exist, read Goldbach’s conjecture.
Also, solve the problem in linear time complexity, i.e., O(n).

Input:
The first line contains T, the number of test cases. The following T lines consist of a number each, for which we'll find two prime numbers.
Note: The number would always be an even number.


Output:
For every test case print two prime numbers space separated, such that the smaller number appears first. Answer for each test case must be in a new line.

Constraints:
1 = T = 70
1 = N = 10000

Example:
Input:
5
74
1024
66
8
9990

Output:
3 71
3 1021
5 61
3 5
17 9973

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
void two_prime_numbers(int ) ;
int main()
 {
int t,no ;
cin>>t ;
while(t--)
{
   cin>>no ;
   two_prime_numbers(no) ;
   cout<<endl ;
}
return 0;
}

void two_prime_numbers(int no)
{
    int arr[no] ;
    int i,j ;
   
    arr[0]=0 ;
    arr[1]=0 ;
    for(i=2;i<=no;i++)
        arr[i]=1 ;
       
    for(i=2;i<=no;i++)
    {
        if(arr[i]==1)
        {
            for(j=i+1;j<=no;j++)
                if(j%i==0)
                    arr[j]=0 ;
        }
    }
   
    for(i=0;i<=no;i++)
    {
        if(arr[i]==1&&arr[no-i]==1)
        {
            cout<<i<<" "<<no-i ;
            break ;
        }
    }
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )