Prime numbers after prime P with sum S

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++

// CPP Program to print all N primes after
// prime P whose sum equals S
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

// vector to store prime and N primes
// whose sum equals given S
vector<int> set;
vector<int> prime;

// function to check prime number
bool isPrime(int x)
{
    // square root of x
    int sqroot = sqrt(x);
    bool flag = true;

    // since 1 is not prime number
    if (x == 1)
        return false;

    // if any factor is found return false
    for (int i = 2; i <= sqroot; i++)
        if (x % i == 0)
            return false;

    // no factor found
    return true;
}

// function to display N primes whose sum equals S
void display()
{
    int length = set.size();
    for (int i = 0; i < length; i++)
        cout << set[i] << " ";
    cout << "\n";
}

// function to evaluate all possible N primes
// whose sum equals S
void primeSum(int total, int N, int S, int index)
{
    // if total equals S And
    // total is reached using N primes
    if (total == S && set.size() == N)
    {
        // display the N primes
        display();
        return;
    }

    // if total is greater than S
    // or if index has reached last element
    if (total > S || index == prime.size())
        return;

    // add prime[index] to set vector
    set.push_back(prime[index]);

    // include the (index)th prime to total
    primeSum(total+prime[index], N, S, index+1);

    // remove element from set vector
    set.pop_back();

    // exclude (index)th prime
    primeSum(total, N, S, index+1);
}

// function to generate all primes
void allPrime(int N, int S, int P)
{
    // all primes less than S itself
    for (int i = P+1; i <=S ; i++)
    {
        // if i is prime add it to prime vector
        if (isPrime(i))
            prime.push_back(i);
    }

    // if primes are less than N
    if (prime.size() < N)
        return;
    primeSum(0, N, S, 0);
}

// Driver Code
int main()
{
    int S = 54, N = 2, P = 3;
    allPrime(N, S, P);
    return 0;
}
Output:

7 47 
11 43 
13 41 
17 37 
23 31

Optimizations :
The above solution can be optimized by pre-computing all required primes using Sieve of Eratosthenes





Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Recommended Posts:



0 Average Difficulty : 0/5.0
No votes yet.