Given a positive integer n, print every Prime Quadruplet below
.
Prime quadruplet: In mathematics, Prime Quadruplet is a set of four primes of the form {p, p+2, p+6, p+8 }.
Example :
Input : N = 15
Output : 5 7 11 13
Input : N = 20
Output : 5 7 11 13
11 13 17 19
A Simple solution to generate all Prime quadruplets up to n is to traverse all positive integer ‘i’ from i=1 to n and check if i, i+2, i+6 and i+8 are primes or not.
An Efficient Solution is to use Sieve Of Eratosthenes to pre-compute all prime numbers in an array in the certain range.
Approach:
- Pre-Compute Prime numbers using Sieve Of Eratosthenes ( refer this )
- Traverse from i=0 to n-7 and check if i, i+2, i+6 and i+8 are also prime or not.
- If yes, Then print i, i+2, i+6, i+8,
Otherwise increment i and check again
Below is the implementation of above idea:
C++
// CPP Program to print prime quadruplet #include <bits/stdc++.h> using namespace std; #define MAX 100000 bool prime[MAX]; // Utility function to generate prime numbers void sieve() { // Sieve Of Eratosthenes for generating // prime number. memset(prime, true, sizeof(prime)); for (int p = 2; p * p < MAX; p++) { // If prime[p] is not changed, // then it is a prime if (prime[p] == true) { // Update all multiples of p for (int i = p * 2; i < MAX; i += p) prime[i] = false; } } } // Function to print Prime quadruplet void printPrimeQuad(int n) { for (int i = 0; i < n - 7; i++) { if (prime[i] && prime[i + 2] && prime[i + 6] && prime[i + 8]) { cout << i << " " << i + 2 << " " << i + 6 << " " << i + 8 << "\n"; } } } // Driver Code int main() { sieve(); int n = 20; printPrimeQuad(20); return 0; } |
Java
// Java code to print prime Quarduplet in a range import java.util.Arrays; import java.util.Collections; class GFG { static final int MAX = 1000000; static boolean[] prime = new boolean[MAX]; // utility function to generate prime number public static void sieve() { // Sieve Of Eratosthenes for generating // prime number. // memset(prime, true, sizeof(prime)); Arrays.fill(prime, true); for (int p = 2; p * p < MAX; p++) { // If prime[p] is not changed, // then it is a prime if (prime[p] == true) { // Update all multiples of p for (int i = p * 2; i < MAX; i += p) prime[i] = false; } } } // function to print prime Quadruplet static void printPrimeQuad(int n) { for (int i = 0; i < n - 7; i++) { if (prime[i] && prime[i + 2] && prime[i + 6] && prime[i + 8]) { System.out.println(i + " " + (i + 2) + " " + (i + 6) + " " + (i + 8)); } } } // Driver Code public static void main(String[] args) { int n = 20; sieve(); printPrimeQuad(n); } } |
Python3
# Python3 Program to print # prime quadruplet # from math lib import sqrt method from math import sqrt MAX = 100000 # Sieve Of Eratosthenes for generating # prime number. prime = [True] * MAX # Utility function to generate # prime numbers def sieve() : for p in range(2, int(sqrt(MAX)) + 1) : # If prime[p] is not changed, # then it is a prime if prime[p] == True : # Update all multiples of p for i in range(p * 2 , MAX, p) : prime[i] = False # Function to print Prime quadruplet def printPrimeQuad(n) : for i in range(n - 7) : if ( prime[i] and prime[i + 2] and prime[i + 6] and prime[i + 8]) : print(i,i + 2,i + 6,i + 8) # Driver code if __name__ == "__main__" : sieve() n = 20 printPrimeQuad(20) # This code is contributed by # ANKITRAI1 |
C#
// C# code to print prime Quarduplet in a range using System; class GFG { const int MAX = 1000000; static bool[] prime = new bool[MAX]; // utility function to generate prime number public static void sieve() { // Sieve Of Eratosthenes for generating // prime number. // memset(prime, true, sizeof(prime)); for(int i=0;i<MAX;i++) prime[i]=true; for (int p = 2; p * p < MAX; p++) { // If prime[p] is not changed, // then it is a prime if (prime[p] == true) { // Update all multiples of p for (int i = p * 2; i < MAX; i += p) prime[i] = false; } } } // function to print prime Quadruplet static void printPrimeQuad(int n) { for (int i = 0; i < n - 7; i++) { if (prime[i] && prime[i + 2] && prime[i + 6] && prime[i + 8]) { Console.WriteLine(i + " " + (i + 2) + " " + (i + 6) + " " + (i + 8)); } } } // Driver Code public static void Main() { int n = 20; sieve(); printPrimeQuad(n); } } |
PHP
<?php // PHP Program to print prime quadruplet $MAX = 100000; // Sieve Of Eratosthenes for generating // prime number. $prime = array_fill(0, $MAX, true); // Utility function to generate // prime numbers function sieve() { global $MAX, $prime; for ($p = 2; $p * $p < $MAX; $p++) { // If prime[p] is not changed, // then it is a prime if ($prime[$p] == true) { // Update all multiples of p for ($i = $p * 2; $i < $MAX; $i += $p) $prime[$i] = false; } } } // Function to print Prime quadruplet function printPrimeQuad($n) { global $MAX, $prime; for ($i = 0; $i < $n - 7; $i++) { if ($prime[$i] && $prime[$i + 2] && $prime[$i + 6] && $prime[$i + 8]) { echo $i . " " . ($i + 2) . " " . ($i + 6) . " " . ($i + 8) . "\n"; } } } // Driver Code sieve(); $n = 20; printPrimeQuad(20); // This code is contributed by mits ?> |
5 7 11 13 11 13 17 19
See Also:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Print all prime numbers less than or equal to N
- Print all Semi-Prime Numbers less than or equal to N
- Print a number strictly less than a given number such that all its digits are distinct.
- Quadruplet pair with XOR zero in the given Array
- Largest number less than N whose each digit is prime number
- Count all the numbers less than 10^6 whose minimum prime factor is N
- Nearest prime less than given number n
- Count pairs with sum as a prime number and less than n
- Sum of largest prime factor of each number less than equal to n
- Find Largest Special Prime which is less than or equal to a given number
- Check if the first and last digit of number N is prime and their sum is less than K
- Number less than equals to N with maximum product of prime factors
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Largest number less than N with digit sum greater than the digit sum of N
- Print all numbers less than N with at-most 2 unique digits
- Print all Strong numbers less than or equal to N
- Print all proper fractions with denominators less than equal to N
- Numbers less than N which are product of exactly two distinct prime numbers
- Sum of Semi-Prime Numbers less than or equal to N
- First element of every K sets having consecutive elements with exactly K prime factors less than N
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

