Given three numbers sum S, prime P, and N, find all N prime numbers after prime P such that their sum is equal to S.
Examples :
Input : N = 2, P = 7, S = 28
Output : 11 17
Explanation : 11 and 17 are primes after
prime 7 and (11 + 17 = 28)
Input : N = 3, P = 2, S = 23
Output : 3 7 13
5 7 11
Explanation : 3, 5, 7, 11 and 13 are primes
after prime 2. And (3 + 7 + 13 = 5 + 7 + 11
= 23)
Input : N = 4, P = 3, S = 54
Output : 5 7 11 31
5 7 13 29
5 7 19 23
5 13 17 19
7 11 13 23
7 11 17 19
Explanation : All are prime numbers and
their sum is 54
Approach: The approach used is to produce all the primes less than S and greater than P. And then backtracking to find if such N primes exist whose sum equals S.
For example, S = 10, N = 2, P = 2

C++
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
vector< int > set;
vector< int > prime;
bool isPrime( int x)
{
int sqroot = sqrt (x);
bool flag = true ;
if (x == 1)
return false ;
for ( int i = 2; i <= sqroot; i++)
if (x % i == 0)
return false ;
return true ;
}
void display()
{
int length = set.size();
for ( int i = 0; i < length; i++)
cout << set[i] << " " ;
cout << "\n" ;
}
void primeSum( int total, int N, int S, int index)
{
if (total == S && set.size() == N)
{
display();
return ;
}
if (total > S || index == prime.size())
return ;
set.push_back(prime[index]);
primeSum(total+prime[index], N, S, index+1);
set.pop_back();
primeSum(total, N, S, index+1);
}
void allPrime( int N, int S, int P)
{
for ( int i = P+1; i <=S ; i++)
{
if (isPrime(i))
prime.push_back(i);
}
if (prime.size() < N)
return ;
primeSum(0, N, S, 0);
}
int main()
{
int S = 54, N = 2, P = 3;
allPrime(N, S, P);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static ArrayList<Integer> set =
new ArrayList<Integer>();
static ArrayList<Integer> prime =
new ArrayList<Integer>();
static boolean isPrime( int x)
{
int sqroot = ( int )Math.sqrt(x);
if (x == 1 )
return false ;
for ( int i = 2 ;
i <= sqroot; i++)
if (x % i == 0 )
return false ;
return true ;
}
static void display()
{
int length = set.size();
for ( int i = 0 ;
i < length; i++)
System.out.print(
set.get(i) + " " );
System.out.println();
}
static void primeSum( int total, int N,
int S, int index)
{
if (total == S &&
set.size() == N)
{
display();
return ;
}
if (total > S ||
index == prime.size() || set.size() >= N)
return ;
set.add(prime.get(index));
primeSum(total + prime.get(index),
N, S, index + 1 );
set.remove(set.size() - 1 );
primeSum(total, N,
S, index + 1 );
}
static void allPrime( int N,
int S, int P)
{
for ( int i = P + 1 ;
i <= S ; i++)
{
if (isPrime(i))
prime.add(i);
}
if (prime.size() < N)
return ;
primeSum( 0 , N, S, 0 );
}
public static void main(String args[])
{
int S = 54 , N = 2 , P = 3 ;
allPrime(N, S, P);
}
}
|
Python3
import math
set = []
prime = []
def isPrime(x) :
sqroot = int (math.sqrt(x))
flag = True
if (x = = 1 ) :
return False
for i in range ( 2 , sqroot + 1 ) :
if (x % i = = 0 ) :
return False
return True
def display() :
global set , prime
length = len ( set )
for i in range ( 0 , length) :
print ( set [i], end = " " )
print ()
def primeSum(total, N,
S, index) :
global set , prime
if (total = = S and
len ( set ) = = N) :
display()
return
if (total > S or
index = = len (prime)) :
return
set .append(prime[index])
primeSum(total + prime[index],
N, S, index + 1 )
set .pop()
primeSum(total, N,
S, index + 1 )
def allPrime(N, S, P) :
global set , prime
for i in range (P + 1 ,
S + 1 ) :
if (isPrime(i)) :
prime.append(i)
if ( len (prime) < N) :
return
primeSum( 0 , N, S, 0 )
S = 54
N = 2
P = 3
allPrime(N, S, P)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static List< int > set = new List< int >();
static List< int > prime = new List< int >();
static bool isPrime( int x)
{
int sqroot = ( int )Math.Sqrt(x);
if (x == 1)
return false ;
for ( int i = 2; i <= sqroot; i++)
if (x % i == 0)
return false ;
return true ;
}
static void display()
{
int length = set .Count;
for ( int i = 0; i < length; i++)
Console.Write( set [i] + " " );
Console.WriteLine();
}
static void primeSum( int total, int N,
int S, int index)
{
if (total == S && set .Count == N)
{
display();
return ;
}
if (total > S || index == prime.Count)
return ;
set .Add(prime[index]);
primeSum(total + prime[index],
N, S, index + 1);
set .RemoveAt( set .Count - 1);
primeSum(total, N, S, index + 1);
}
static void allPrime( int N,
int S, int P)
{
for ( int i = P + 1; i <=S ; i++)
{
if (isPrime(i))
prime.Add(i);
}
if (prime.Count < N)
return ;
primeSum(0, N, S, 0);
}
static void Main()
{
int S = 54, N = 2, P = 3;
allPrime(N, S, P);
}
}
|
PHP
<?php
$set = array ();
$prime = array ();
function isPrime( $x )
{
$sqroot = sqrt( $x );
$flag = true;
if ( $x == 1)
return false;
for ( $i = 2; $i <= $sqroot ; $i ++)
if ( $x % $i == 0)
return false;
return true;
}
function display()
{
global $set , $prime ;
$length = count ( $set );
for ( $i = 0; $i < $length ; $i ++)
echo ( $set [ $i ] . " " );
echo ( "\n" );
}
function primeSum( $total , $N ,
$S , $index )
{
global $set , $prime ;
if ( $total == $S &&
count ( $set ) == $N )
{
display();
return ;
}
if ( $total > $S ||
$index == count ( $prime ))
return ;
array_push ( $set ,
$prime [ $index ]);
primeSum( $total + $prime [ $index ],
$N , $S , $index + 1);
array_pop ( $set );
primeSum( $total , $N , $S ,
$index + 1);
}
function allPrime( $N , $S , $P )
{
global $set , $prime ;
for ( $i = $P + 1;
$i <= $S ; $i ++)
{
if (isPrime( $i ))
array_push ( $prime , $i );
}
if ( count ( $prime ) < $N )
return ;
primeSum(0, $N , $S , 0);
}
$S = 54; $N = 2; $P = 3;
allPrime( $N , $S , $P );
?>
|
Javascript
<script>
var set = [];
var prime = [];
function isPrime(x)
{
var sqroot = Math.sqrt(x);
var flag = true ;
if (x == 1)
return false ;
for ( var i = 2; i <= sqroot; i++)
if (x % i == 0)
return false ;
return true ;
}
function display()
{
var length = set.length;
for ( var i = 0; i < length; i++)
document.write( set[i] + " " );
document.write( "<br>" );
}
function primeSum(total, N, S, index)
{
if (total == S && set.length == N)
{
display();
return ;
}
if (total > S || index == prime.length)
return ;
set.push(prime[index]);
primeSum(total+prime[index], N, S, index+1);
set.pop();
primeSum(total, N, S, index+1);
}
function allPrime(N, S, P)
{
for ( var i = P+1; i <=S ; i++)
{
if (isPrime(i))
prime.push(i);
}
if (prime.length < N)
return ;
primeSum(0, N, S, 0);
}
var S = 54, N = 2, P = 3;
allPrime(N, S, P);
</script>
|
Output: 7 47
11 43
13 41
17 37
23 31
The time complexity of this algorithm is O(n^2) where n is the number of prime numbers between P and S. This is because the nested for loop in the allPrime() function runs for O(n) and the recursive primeSum() function runs for O(n) time as well.
The space complexity is O(n) because we are using a vector of size n to store prime numbers and another vector of size n to store a possible combination of N primes.
Optimizations :
The above solution can be optimized by pre-computing all required primes using Sieve of Eratosthenes