Prime triplets consisting of values up to N having difference between two elements equal to the third
Last Updated :
16 Jun, 2021
Given a positive integer N, the task is to find all the prime triplets {P, Q, R} such that P = R – Q and P, Q and R is less than N.
Examples:
Input: N = 8
Output:
2 3 5
2 5 7
Explanation:
The only 2 prime triplets satisfying the given conditions are:
- {2, 3, 5}: P = 2, Q = 3, R = 5. Therefore, P, Q and R are prime numbers and P = R – Q.
- {2, 5, 7}: P = 2, Q = 5, R = 7. Therefore, P, Q and R are prime numbers and P = R – Q.
Input: N = 5
Output: 2 3 5
Approach: The given problem can be solved based on the following observations:
- By rearranging the given equation, it can be observed that P + Q = R, and the sum of two odd numbers is even and the sum of one odd and one even is odd.
- As there is only one even prime i.e., 2. Let P is odd prime and Q is odd prime then R can never be a prime number, so it is necessary that P should be always 2 which is even prime and Q is odd prime then R should be an odd prime. So there is necessary to find prime Q such that Q > 2 and R = P + Q ≤ N (where P = 2) and R should be prime.
Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool prime[100000];
void SieveOfEratosthenes( int n)
{
memset (prime, true , sizeof (prime));
for ( int p = 2; p * p <= n; p++) {
if (prime[p] == true ) {
for ( int i = p * p;
i <= n; i += p) {
prime[i] = false ;
}
}
}
}
void findTriplets( int N)
{
SieveOfEratosthenes(N);
vector<vector< int > > V;
for ( int i = 3; i <= N; i++) {
if (2 + i <= N && prime[i]
&& prime[2 + i]) {
V.push_back({ 2, i, i + 2 });
}
}
for ( int i = 0; i < V.size(); i++) {
cout << V[i][0] << " "
<< V[i][1] << " "
<< V[i][2] << "\n" ;
}
}
int main()
{
int N = 8;
findTriplets(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean [] prime = new boolean [ 100000 ];
static void initialize()
{
for ( int i = 0 ; i < 100000 ; i++)
prime[i] = true ;
}
static void SieveOfEratosthenes( int n)
{
for ( int p = 2 ; p * p <= n; p++)
{
if (prime[p] == true )
{
for ( int i = p * p; i <= n; i += p)
{
prime[i] = false ;
}
}
}
}
static void findTriplets( int N)
{
SieveOfEratosthenes(N);
ArrayList<ArrayList<Integer>> V = new ArrayList<ArrayList<Integer>>();
for ( int i = 3 ; i <= N; i++)
{
if ( 2 + i <= N && prime[i] && prime[ 2 + i])
{
ArrayList<Integer> a1 = new ArrayList<Integer>();
a1.add( 2 );
a1.add(i);
a1.add(i + 2 );
V.add(a1);
}
}
for ( int i = 0 ; i < V.size(); i++)
{
System.out.println(V.get(i).get( 0 ) + " " +
V.get(i).get( 1 ) + " " +
V.get(i).get( 2 ));
}
}
public static void main(String args[])
{
initialize();
int N = 8 ;
findTriplets(N);
}
}
|
Python3
from math import sqrt
prime = [ True for i in range ( 100000 )]
def SieveOfEratosthenes(n):
for p in range ( 2 , int (sqrt(n)) + 1 , 1 ):
if (prime[p] = = True ):
for i in range (p * p, n + 1 , p):
prime[i] = False
def findTriplets(N):
SieveOfEratosthenes(N)
V = []
for i in range ( 3 , N + 1 , 1 ):
if ( 2 + i < = N and prime[i] and prime[ 2 + i]):
V.append([ 2 , i, i + 2 ])
for i in range ( len (V)):
print (V[i][ 0 ], V[i][ 1 ], V[i][ 2 ])
if __name__ = = '__main__' :
N = 8
findTriplets(N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static bool [] prime = new bool [100000];
static void initialize()
{
for ( int i = 0; i < 100000; i++)
prime[i] = true ;
}
static void SieveOfEratosthenes( int n)
{
for ( int p = 2; p * p <= n; p++)
{
if (prime[p] == true )
{
for ( int i = p * p; i <= n; i += p)
{
prime[i] = false ;
}
}
}
}
static void findTriplets( int N)
{
SieveOfEratosthenes(N);
List<List< int >> V = new List<List< int >>();
for ( int i = 3; i <= N; i++)
{
if (2 + i <= N && prime[i] ==
true && prime[2 + i])
{
List< int > a1 = new List< int >();
a1.Add(2);
a1.Add(i);
a1.Add(i + 2);
V.Add(a1);
}
}
for ( int i = 0; i < V.Count; i++)
{
Console.WriteLine(V[i][0] + " " +
V[i][1] + " " +
V[i][2]);
}
}
public static void Main()
{
initialize();
int N = 8;
findTriplets(N);
}
}
|
Javascript
<script>
var prime = new Array(100000);
function SieveOfEratosthenes(n)
{
prime.fill( true );
for ( var p = 2; p * p <= n; p++)
{
if (prime[p] == true )
{
for ( var i = p * p;
i <= n; i += p)
{
prime[i] = false ;
}
}
}
}
function findTriplets(N)
{
SieveOfEratosthenes(N);
var V = [];
for ( var i = 3; i <= N; i++)
{
if (2 + i <= N && prime[i] == true &&
prime[2 + i] == true )
{
var a1 = [2, i, i + 2];
V.push(a1);
}
}
for ( var i = 0; i < V.length; i++)
{
document.write(V[i][0] + " " +
V[i][1] + " " +
V[i][2] + "<br>" );
}
}
N = 8;
findTriplets(N);
</script>
|
Time Complexity: O(N*log(log(N)))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...