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:
- Precompute all the prime numbers over the range [1, N] using Sieve of Eratosthenes.
- Initialize a vector of vectors, say V, to stores all the resultant triplets.
- Iterate over the range [3, N] using a variable, say i. If i and (i + 2) are prime and 2 + i ≤ N, then store the current triplets in the vector V.
- After completing the above steps, print all the triplets stored in V[].
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;// Stores 1 and 0 at indices which// are prime and non-prime respectivelybool prime[100000];// Function to find all prime// numbers from the range [0, N]void SieveOfEratosthenes(int n){ // Consider all numbers to prime initially memset(prime, true, sizeof(prime)); // Iterate over the range [2, sqrt(N)] for (int p = 2; p * p <= n; p++) { // If p is a prime if (prime[p] == true) { // Update all tultiples // of p as false for (int i = p * p; i <= n; i += p) { prime[i] = false; } } }}// Function to find all prime triplets// satisfying the given conditionsvoid findTriplets(int N){ // Generate all primes up to N SieveOfEratosthenes(N); // Stores the triplets vector<vector<int> > V; // Iterate over the range [3, N] for (int i = 3; i <= N; i++) { // Check for the condition if (2 + i <= N && prime[i] && prime[2 + i]) { // Store the triplets V.push_back({ 2, i, i + 2 }); } } // Print all the stored triplets for (int i = 0; i < V.size(); i++) { cout << V[i][0] << " " << V[i][1] << " " << V[i][2] << "\n"; }}// Driver Codeint main(){ int N = 8; findTriplets(N); return 0;} |
Java
// Java program for the above approachimport java.util.*;class GFG{// Stores 1 and 0 at indices which// are prime and non-prime respectivelystatic boolean[] prime = new boolean[100000];static void initialize(){ for(int i = 0; i < 100000; i++) prime[i] = true;}// Function to find all prime// numbers from the range [0, N]static void SieveOfEratosthenes(int n){ // Iterate over the range [2, sqrt(N)] for(int p = 2; p * p <= n; p++) { // If p is a prime if (prime[p] == true) { // Update all tultiples // of p as false for(int i = p * p; i <= n; i += p) { prime[i] = false; } } }}// Function to find all prime triplets// satisfying the given conditionsstatic void findTriplets(int N){ // Generate all primes up to N SieveOfEratosthenes(N); // Stores the triplets ArrayList<ArrayList<Integer>> V = new ArrayList<ArrayList<Integer>>(); // List<List<int> > V = new List<List<int>>(); // Iterate over the range [3, N] for(int i = 3; i <= N; i++) { // Check for the condition if (2 + i <= N && prime[i] && prime[2 + i]) { // Store the triplets ArrayList<Integer> a1 = new ArrayList<Integer>(); a1.add(2); a1.add(i); a1.add(i + 2); V.add(a1); } } // Print all the stored triplets 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)); }}// Driver Codepublic static void main(String args[]){ initialize(); int N = 8; findTriplets(N);}}// This code is contributed by ipg2016107 |
Python3
# Python3 program for the above approachfrom math import sqrt# Stores 1 and 0 at indices which# are prime and non-prime respectivelyprime = [True for i in range(100000)]# Function to find all prime# numbers from the range [0, N]def SieveOfEratosthenes(n): # Iterate over the range [2, sqrt(N)] for p in range(2, int(sqrt(n)) + 1, 1): # If p is a prime if (prime[p] == True): # Update all tultiples # of p as false for i in range(p * p, n + 1, p): prime[i] = False# Function to find all prime triplets# satisfying the given conditionsdef findTriplets(N): # Generate all primes up to N SieveOfEratosthenes(N) # Stores the triplets V = [] # Iterate over the range [3, N] for i in range(3, N + 1, 1): # Check for the condition if (2 + i <= N and prime[i] and prime[2 + i]): # Store the triplets V.append([2, i, i + 2]) # Print all the stored triplets for i in range(len(V)): print(V[i][0], V[i][1], V[i][2])# Driver Codeif __name__ == '__main__': N = 8 findTriplets(N) # This code is contributed by bgangwar59. |
C#
// C# program for the above approachusing System;using System.Collections.Generic;class GFG{// Stores 1 and 0 at indices which// are prime and non-prime respectivelystatic bool[] prime = new bool[100000];static void initialize(){ for(int i = 0; i < 100000; i++) prime[i] = true;}// Function to find all prime// numbers from the range [0, N]static void SieveOfEratosthenes(int n){ // Iterate over the range [2, sqrt(N)] for(int p = 2; p * p <= n; p++) { // If p is a prime if (prime[p] == true) { // Update all tultiples // of p as false for(int i = p * p; i <= n; i += p) { prime[i] = false; } } }}// Function to find all prime triplets// satisfying the given conditionsstatic void findTriplets(int N){ // Generate all primes up to N SieveOfEratosthenes(N); // Stores the triplets List<List<int>> V = new List<List<int>>(); // Iterate over the range [3, N] for(int i = 3; i <= N; i++) { // Check for the condition if (2 + i <= N && prime[i] == true && prime[2 + i]) { // Store the triplets List<int> a1 = new List<int>(); a1.Add(2); a1.Add(i); a1.Add(i + 2); V.Add(a1); } } // Print all the stored triplets for(int i = 0; i < V.Count; i++) { Console.WriteLine(V[i][0] + " " + V[i][1] + " " + V[i][2]); }}// Driver Codepublic static void Main(){ initialize(); int N = 8; findTriplets(N);}}// This code is contributed by SURENDRA_GANGWAR |
Javascript
<script>// Javascript program for the above approach// Stores 1 and 0 at indices which// are prime and non-prime respectivelyvar prime = new Array(100000);// Function to find all prime// numbers from the range [0, N]function SieveOfEratosthenes(n){ // Consider all numbers to prime initially prime.fill(true); // Iterate over the range [2, sqrt(N)] for(var p = 2; p * p <= n; p++) { // If p is a prime if (prime[p] == true) { // Update all tultiples // of p as false for(var i = p * p; i <= n; i += p) { prime[i] = false; } } }}// Function to find all prime triplets// satisfying the given conditionsfunction findTriplets(N){ // Generate all primes up to N SieveOfEratosthenes(N); // Stores the triplets var V = []; // Iterate over the range [3, N] for(var i = 3; i <= N; i++) { // Check for the condition if (2 + i <= N && prime[i] == true && prime[2 + i] == true) { // Store the triplets var a1 = [2, i, i + 2]; V.push(a1); } } // Print all the stored triplets for(var i = 0; i < V.length; i++) { document.write(V[i][0] + " " + V[i][1] + " " + V[i][2] + "<br>"); }}// Driver codeN = 8;findTriplets(N);// This code is contributed by SoumikMondal</script> |
2 3 5 2 5 7
Time Complexity: O(N*log(log(N)))
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


