Given an integer ‘sum’ (less than 10^8), the task is to find a pair of prime numbers whose sum is equal to the given ‘sum’
Out of all the possible pairs, the absolute difference between the chosen pair must be minimum.
If the ‘sum’ cannot be represented as a sum of two prime numbers then print “Cannot be represented as sum of two primes”.
Examples:
Input : Sum = 1002
Output : Primes: 499 503
Explanation
1002 can be represented as sum of many prime number pairs
such as
499 503
479 523
461 541
439 563
433 569
431 571
409 593
401 601...
But 499 and 503 is the only pair which has minimum difference
Input :Sum = 2002
Output : Primes: 983 1019
Solution
- We will create a sieve of Eratosthenes which will store all the prime numbers and check whether a number is prime or not in O(1) time.
- Now, to find two prime numbers with sum equal to the given variable, ‘sum’. We will start a loop from sum/2 to 1 (to minimize the absolute difference) and check whether the loop counter ‘i’ and ‘sum-i’ are both prime.
- If they are prime then we will print them and break out of the loop.
- If the ‘sum’ cannot be represented as a sum of two prime numbers then we will print “Cannot be represented as sum of two primes”.
Below is the implementation of the above solution:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 100000000
bool prime[MAX + 1];
void SieveOfEratosthenes()
{
memset (prime, true , sizeof (prime));
prime[1] = false ;
for ( int p = 2; p * p <= MAX; p++) {
if (prime[p] == true ) {
for ( int i = p * 2; i <= MAX; i += p)
prime[i] = false ;
}
}
}
void find_Prime( int sum)
{
for ( int i = sum / 2; i > 1; i--) {
if (prime[i] && prime[sum - i]) {
cout << i << " " << (sum - i) << endl;
return ;
}
}
cout << "Cannot be represented as sum of two primes" << endl;
}
int main()
{
SieveOfEratosthenes();
int sum = 1002;
find_Prime(sum);
return 0;
}
|
Java
class GFG {
static final int MAX = 100000000 ;
static boolean prime[] = new boolean [MAX + 1 ];
static void SieveOfEratosthenes() {
for ( int i = 0 ; i < prime.length; i++) {
prime[i] = true ;
}
prime[ 1 ] = false ;
for ( int p = 2 ; p * p <= MAX; p++) {
if (prime[p] == true ) {
for ( int i = p * 2 ; i <= MAX; i += p) {
prime[i] = false ;
}
}
}
}
static void find_Prime( int sum) {
for ( int i = sum / 2 ; i > 1 ; i--) {
if (prime[i] && prime[sum - i]) {
System.out.println(i + " " + (sum - i));
return ;
}
}
System.out.println( "Cannot be represented as sum of two primes" );
}
public static void main(String []args) {
SieveOfEratosthenes();
int sum = 1002 ;
find_Prime(sum);
}
}
|
Python3
from math import sqrt
def SieveOfEratosthenes():
MAX = 1000001
prime = [ True for i in range ( MAX + 1 )]
prime[ 1 ] = False
for p in range ( 2 , int (sqrt( MAX )) + 1 , 1 ):
if (prime[p] = = True ):
for i in range (p * 2 , MAX + 1 , p):
prime[i] = False
return prime
def find_Prime( sum ):
prime = SieveOfEratosthenes()
i = int ( sum / 2 )
while (i > 1 ):
if (prime[i] and prime[ sum - i]):
print (i, ( sum - i))
return
i - = 1
print ( "Cannot be represented as sum" ,
"of two primes" )
if __name__ = = '__main__' :
sum = 1002
find_Prime( sum )
|
C#
class GFG
{
static int MAX = 1000000;
static bool [] prime = new bool [MAX + 1];
static void SieveOfEratosthenes()
{
for ( int i = 0; i < prime.Length; i++)
{
prime[i] = true ;
}
prime[1] = false ;
for ( int p = 2; p * p <= MAX; p++)
{
if (prime[p] == true )
{
for ( int i = p * 2;
i <= MAX; i += p)
{
prime[i] = false ;
}
}
}
}
static void find_Prime( int sum)
{
for ( int i = sum / 2; i > 1; i--)
{
if (prime[i] && prime[sum - i])
{
System.Console.WriteLine(i + " " +
(sum - i));
return ;
}
}
System.Console.WriteLine( "Cannot be represented " +
"as sum of two primes" );
}
static void Main()
{
SieveOfEratosthenes();
int sum = 1002;
find_Prime(sum);
}
}
|
Javascript
<script>
var MAX = 1000001;
var prime = Array(MAX+1).fill( true );
function SieveOfEratosthenes()
{
prime[1] = false ;
for ( var p = 2; p * p <= MAX; p++) {
if (prime[p] == true ) {
for ( var i = p * 2; i <= MAX; i += p)
prime[i] = false ;
}
}
}
function find_Prime(sum)
{
for ( var i = parseInt(sum / 2); i > 1; i--) {
if (prime[i] && prime[sum - i]) {
document.write( i + " " + (sum - i) + "<br>" );
return ;
}
}
document.write( "Cannot be represented as sum of two primes" + "<br>" );
}
SieveOfEratosthenes();
var sum = 1002;
find_Prime(sum);
</script>
|
Time Complexity: O(n + MAX3/2)
Auxiliary Space: O(MAX)