Pair of primes: PAIRPR1
link: https://www.codechef.com/CU1AP0006/problems/PAIRPR1
Given an integer nn (between 11 and 104104) find two prime numbers (possibly same) p1,p2p1,p2 such that p1+p2=np1+p2=n
In case there are multiple solutions, you can output any of them.
If there is no solution, then print -1 -1
instead.
Solution:
#include <bits/stdc++.h>
#define endl "\n"
#define ll long long int
using namespace std;
ll T = 1;
bool isPrime(ll n)
{
for (ll i = 2; i*i <= n ; i++)
{
if (n % i == 0)
return false;
}
return true;
}
int solve()
{
ll n;
cin >> n;
if (n <= 3)
{
cout << -1 << " " << -1 << endl;
return 0;
}
for (int i = 2; i <= n/2 ; i++)
{
if (isPrime(i) && isPrime(n - i))
{
cout << i << " " << n - i << endl;
return 0;
}
}
cout << -1 << " " << -1 << endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> T;
while (T--)
{
solve();
}
return 0;
}