Count pairs with sum as a prime number and less than n

Given a positive integer n, count distinct number of pairs (x, y) that satisfy following conditions :

  • (x + y) is a prime number.
  • (x + y) < n
  • x != y
  • 1 <= x, y

Examples:

Input : n = 6
Output : 3
prime pairs whose sum is less than 6 are:
(1,2), (1,4), (2,3) 

Input : 12
Output : 11
prime pairs whose sum is less than 12 are:
(1,2),  (1,4), (2,3), (1,6), (2,5), (3,4), 
(1,10), (2,9), (3,8), (4,7), (5,6)

Approach:

1) Find all prime numbers less than n using
   Sieve of Sundaram

2) For each prime number p, count distinct
   pairs that sum up to p.
   
For any odd number n, number of distinct pairs
that add upto n are n/2
Since, a prime number is a odd number, the 
same applies for it too. 

Example,
For prime number p = 7
distinct pairs that add upto p: p/2 = 7/2 = 3
The three pairs are (1,6), (2,5), (3,4)

For prime number p = 23
distinct pairs that add upto p: p/2 = 23/2 = 11

// C++ implementation of prime pairs
// whose sum is less than n
#include <bits/stdc++.h>
using namespace std;

// Sieve of Sundaram for generating
// prime numbers less than n
void SieveOfSundaram(bool marked[], int nNew)
{
    // Main logic of Sundaram.  Mark all numbers
    // of the form i + j + 2ij as true where
    // 1 <= i <= j
    for (int i=1; i<=nNew; i++)
        for (int j=i; (i + j + 2*i*j) <= nNew; j++)
            marked[i + j + 2*i*j] = true;
}

// Returns number of pairs with fiven conditions.
int countPrimePairs(int n)
{
    // In general Sieve of Sundaram, produces
    // primes smaller than (2*x + 2) for a number
    // given number x. Since we want primes smaller
    // than n, we reduce n to half
    int nNew = (n-2)/2;

    // This array is used to separate numbers of
    // the form i+j+2ij from others where
    // 1 <= i <= j
    bool marked[nNew + 1];

    // Initialize all elements as not marked
    memset(marked, false, sizeof(marked));

    SieveOfSundaram(marked, nNew);

    int count = 0, prime_num;

    // Find primes. Primes are of the form
    // 2*i + 1 such that marked[i] is false.
    for (int i=1; i<=nNew; i++)
    {
        if (marked[i] == false)
        {
            prime_num = 2*i + 1;

            // For a given prime number p
            // number of distinct pairs(i,j)
            // where (i+j) = p are p/2
            count = count + (prime_num / 2);
        }
    }

    return count;
}

// Driver program to test above
int main(void)
{
    int n = 12;
    cout << "Number of prime pairs: "
         << countPrimePairs(n);
    return 0;
}

Output:

Number of prime pairs: 11

This article is contributed by Ayush Jauhari. 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.

GATE CS Corner    Company Wise Coding Practice





Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.