Given an integer N, the task is to count the number of pairs of prime numbers in the range [1, N] such that the difference between elements of each pair is also a prime number.
Examples:
Input: N = 5
Output: 2
Explanations:
Pair of prime numbers in the range [1, 5] whose difference between elements is also a prime number are:
(2, 5) = 3 (Prime number)
(3, 5) = 2 (Prime number)
Therefore, the count of pairs of the prime numbers whose difference is also a prime number is 2.
Input: N = 11
Output: 4
Naive Approach: The simplest approach to solve this problem is to generate all possible pairs of the elements in the range [1, N] and for each pair, check if both the elements and the difference between both the elements of the pair is a prime number or not. If found to be true then increment the count. Finally, print the count.
Time Complexity: O(N2 * √N)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is based on the following observations:
Odd number – Even Number = Odd Number
Odd number – Odd number = Even number
2 is the only even prime number.
Therefore, the problem reduces to check only for those pairs of prime numbers whose difference between the elements of the pair is equal to 2.
Follow the steps below to solve the problem:
- Initialize a variable, say cntPairs to store the count of pairs of prime numbers such that the difference between elements of each pair is also a prime number.
- Initialize an array, say sieve[] to check if a number in the range [1, N] is a prime number or not.
- Find all the prime numbers in the range [1, N] using Sieve of Eratosthenes.
- Iterate over the range [2, N], and for each element in the given range, check if the sieve[i] and sieve[i – 2] are true or not. If found to be true then increment the value of cntPairs by 2.
- Finally, print the value of cntPairs.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector< bool > SieveOfEratosthenes(
int N)
{
vector< bool > isPrime(N, true );
isPrime[0] = false ;
isPrime[1] = false ;
for ( int p = 2; p * p <= N; p++) {
if (isPrime[p]) {
for ( int i = p * p; i <= N;
i += p) {
isPrime[i] = false ;
}
}
}
return isPrime;
}
int cntPairsdiffOfPrimeisPrime( int N)
{
int cntPairs = 0;
vector< bool > isPrime
= SieveOfEratosthenes(N);
for ( int i = 2; i <= N; i++) {
if (isPrime[i] &&
isPrime[i - 2]) {
cntPairs += 2;
}
}
return cntPairs;
}
int main()
{
int N = 5;
cout << cntPairsdiffOfPrimeisPrime(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
public static boolean [] SieveOfEratosthenes( int N)
{
boolean [] isPrime = new boolean [N + 1 ];
Arrays.fill(isPrime, true );
isPrime[ 0 ] = false ;
isPrime[ 1 ] = false ;
for ( int p = 2 ; p * p <= N; p++)
{
if (isPrime[p])
{
for ( int i = p * p; i <= N; i += p)
{
isPrime[i] = false ;
}
}
}
return isPrime;
}
public static int cntPairsdiffOfPrimeisPrime( int N)
{
int cntPairs = 0 ;
boolean [] isPrime = SieveOfEratosthenes(N);
for ( int i = 2 ; i <= N; i++)
{
if (isPrime[i] && isPrime[i - 2 ])
{
cntPairs += 2 ;
}
}
return cntPairs;
}
public static void main(String args[])
{
int N = 5 ;
System.out.println(cntPairsdiffOfPrimeisPrime(N));
}
}
|
Python3
from math import sqrt
def SieveOfEratosthenes(N):
isPrime = [ True for i in range (N + 1 )]
isPrime[ 0 ] = False
isPrime[ 1 ] = False
for p in range ( 2 , int (sqrt(N)) + 1 , 1 ):
if (isPrime[p]):
for i in range (p * p, N + 1 , p):
isPrime[i] = False
return isPrime
def cntPairsdiffOfPrimeisPrime(N):
cntPairs = 0
isPrime = SieveOfEratosthenes(N)
for i in range ( 2 , N + 1 , 1 ):
if (isPrime[i] and isPrime[i - 2 ]):
cntPairs + = 2
return cntPairs
if __name__ = = '__main__' :
N = 5
print (cntPairsdiffOfPrimeisPrime(N))
|
C#
using System;
class GFG{
public static bool [] SieveOfEratosthenes( int N)
{
bool [] isPrime = new bool [N + 1];
for ( int i = 0; i < N + 1; i++)
{
isPrime[i] = true ;
}
isPrime[0] = false ;
isPrime[1] = false ;
for ( int p = 2; p * p <= N; p++)
{
if (isPrime[p])
{
for ( int i = p * p; i <= N; i += p)
{
isPrime[i] = false ;
}
}
}
return isPrime;
}
public static int cntPairsdiffOfPrimeisPrime( int N)
{
int cntPairs = 0;
bool [] isPrime = SieveOfEratosthenes(N);
for ( int i = 2; i <= N; i++)
{
if (isPrime[i] && isPrime[i - 2])
{
cntPairs += 2;
}
}
return cntPairs;
}
public static void Main()
{
int N = 5;
Console.WriteLine(cntPairsdiffOfPrimeisPrime(N));
}
}
|
Javascript
<script>
function SieveOfEratosthenes(N)
{
let isPrime = [];
for (let i = 0; i < N + 1; i++)
{
isPrime[i] = true ;
}
isPrime[0] = false ;
isPrime[1] = false ;
for (let p = 2; p * p <= N; p++)
{
if (isPrime[p])
{
for (let i = p * p; i <= N; i += p)
{
isPrime[i] = false ;
}
}
}
return isPrime;
}
function cntPairsdiffOfPrimeisPrime(N)
{
let cntPairs = 0;
let isPrime = SieveOfEratosthenes(N);
for (let i = 2; i <= N; i++)
{
if (isPrime[i] && isPrime[i - 2])
{
cntPairs += 2;
}
}
return cntPairs;
}
let N = 5;
document.write(cntPairsdiffOfPrimeisPrime(N));
</script>
|
Time Complexity: O(N * log(log(N)))
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
23 Apr, 2021
Like Article
Save Article