# Prime Triplet

Prime Triplet is a set of three prime numbers of the form (p, p+2, p+6) or (p, p+4, p+6). This is the closest possible grouping of three prime numbers, since one of every three sequential odd numbers is a multiple of three, and hence not prime (except for 3 itself) except (2, 3, 5) and (3, 5, 7) .

Examples :

```Input : n = 15
Output : 5 7 11
7 11 13

Input : n = 25
Output : 5 7 11
7 11 13
11 13 17
13 17 19
17 19 23
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

A simple solution is to traverse through all numbers from 1 to n-6. For every number i check if i, i+2, i+6 or i, i+4, i+6 are primes. If yes, print triplet.

An efficient solution is to use Sieve of Eratosthenes to first find all prime numbers so that we can quickly check if a number is prime or not.

Below is the implementation of the approach.

## C++

 `// C++ program to find prime triplets smaller ` `// than or equal to n. ` `#include ` `using` `namespace` `std; ` ` `  `// function to detect prime number ` `// here we have used sieve method ` `// https://www.geeksforgeeks.org/sieve-of-eratosthenes/ ` `// to detect prime number ` `void` `sieve(``int` `n, ``bool` `prime[]) ` `{ ` `    ``for` `(``int` `p = 2; p * p <= n; 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 <= n; i += p) ` `                ``prime[i] = ``false``; ` `        ``} ` `    ``} ` `} ` ` `  `// function to print prime triplets ` `void` `printPrimeTriplets(``int` `n) ` `{ ` `    ``// Finding all primes from 1 to n ` `    ``bool` `prime[n + 1]; ` `    ``memset``(prime, ``true``, ``sizeof``(prime)); ` `    ``sieve(n, prime); ` `     `  `    ``cout << ``"The prime triplets from 1 to "`  `          ``<< n << ``"are :"` `<< endl; ` `    ``for` `(``int` `i = 2; i <= n-6; ++i) { ` ` `  `        ``// triplets of form (p, p+2, p+6) ` `        ``if` `(prime[i] && prime[i + 2] && prime[i + 6]) ` `            ``cout << i << ``" "` `<< (i + 2) << ``" "` `<< (i + 6) << endl; ` ` `  `        ``// triplets of form (p, p+4, p+6) ` `        ``else` `if` `(prime[i] && prime[i + 4] && prime[i + 6]) ` `            ``cout << i << ``" "` `<< (i + 4) << ``" "` `<< (i + 6) << endl; ` `    ``} ` `} ` ` `  `int` `main() ` `{ ` `    ``int` `n = 25; ` `    ``printPrimeTriplets(n); ` `    ``return` `0; ` `} `

## Java

 `// Java program to find prime triplets ` `// smaller than or equal to n. ` `import` `java.io.*; ` `import` `java.util.*; ` ` `  `class` `GFG { ` `     `  `// function to detect prime number ` `// here we have used sieve method ` `// https://www.geeksforgeeks.org/sieve-of-eratosthenes/ ` `// to detect prime number ` `    ``static` `void` `sieve(``int` `n, ``boolean` `prime[]) ` `    ``{ ` `        ``for` `(``int` `p = ``2``; p * p <= n; 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 <= n; i += p) ` `                    ``prime[i] = ``false``; ` `            ``} ` `        ``} ` `    ``} ` `     `  `    ``// function to print prime triplets ` `    ``static` `void` `printPrimeTriplets(``int` `n) ` `    ``{ ` `        ``// Finding all primes from 1 to n ` `        ``boolean` `prime[]=``new` `boolean``[n + ``1``]; ` `        ``Arrays.fill(prime,``true``); ` `        ``sieve(n, prime); ` `         `  `        ``System.out.println(``"The prime triplets"``+ ` `                           ``" from 1 to "` `+ n + ``"are :"``); ` `         `  `        ``for` `(``int` `i = ``2``; i <= n-``6``; ++i) { ` `     `  `            ``// triplets of form (p, p+2, p+6) ` `            ``if` `(prime[i] && prime[i + ``2``] && prime[i + ``6``]) ` `                ``System.out.println( i + ``" "` `+ (i + ``2``) +  ` `                                    ``" "` `+ (i + ``6``)); ` `     `  `            ``// triplets of form (p, p+4, p+6) ` `            ``else` `if` `(prime[i] && prime[i + ``4``] &&  ` `                     ``prime[i + ``6``]) ` `                 `  `                ``System.out.println(i + ``" "` `+ (i + ``4``) + ` `                                   ``" "` `+ (i + ``6``)); ` `        ``} ` `    ``} ` `     `  `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``int` `n = ``25``; ` `        ``printPrimeTriplets(n); ` `    ``} ` `} ` ` `  ` `  ` ``/*This code is contributed by Nikita Tiwari.*/`

## Python3

 `# Python 3 program to find  ` `# prime triplets smaller ` `# than or equal to n. ` ` `  `# function to detect prime number ` `# using sieve method ` `# https://www.geeksforgeeks.org/sieve-of-eratosthenes/ ` `# to detect prime number ` `def` `sieve(n, prime) : ` `     `  `    ``p ``=` `2` `     `  `    ``while` `(p ``*` `p <``=` `n ) : ` `         `  `        ``# If prime[p] is not changed ` `        ``# , then it is a prime ` `        ``if` `(prime[p] ``=``=` `True``) : ` `             `  `            ``# Update all multiples of p ` `            ``i ``=` `p ``*` `2` `         `  `            ``while` `( i <``=` `n ) : ` `                ``prime[i] ``=` `False` `                ``i ``=` `i ``+` `p ` `         `  `        ``p ``=` `p ``+` `1` `         `  ` `  `# function to print  ` `# prime triplets ` `def` `printPrimeTriplets(n) : ` ` `  `    ``# Finding all primes  ` `    ``# from 1 to n ` `    ``prime ``=` `[``True``] ``*` `(n ``+` `1``) ` `    ``sieve(n, prime) ` `     `  `    ``print``( ``"The prime triplets from 1 to "``, ` `                               ``n , ``"are :"``) ` `     `  `    ``for` `i ``in` `range``(``2``, n ``-` `6` `+` `1``) : ` `         `  `        ``# triplets of form (p, p+2, p+6) ` `        ``if` `(prime[i] ``and` `prime[i ``+` `2``] ``and` `                            ``prime[i ``+` `6``]) : ` `            ``print``( i , (i ``+` `2``) , (i ``+` `6``)) ` `             `  `        ``# triplets of form (p, p+4, p+6) ` `        ``elif` `(prime[i] ``and` `prime[i ``+` `4``] ``and` `                            ``prime[i ``+` `6``]) : ` `            ``print``(i , (i ``+` `4``) , (i ``+` `6``)) ` `             `  `# Driver code ` `n ``=` `25` `printPrimeTriplets(n) ` ` `  `# This code is contributed by Nikita Tiwari. `

## C#

 `// C# program to find prime  ` `// triplets smaller than or ` `// equal to n. ` `using` `System; ` ` `  `class` `GFG  ` `{ ` `     `  `// function to detect  ` `// prime number ` `static` `void` `sieve(``int` `n,  ` `                  ``bool``[] prime) ` `{ ` `    ``for` `(``int` `p = 2;  ` `             ``p * p <= n; p++)  ` `    ``{ ` ` `  `        ``// If prime[p] is not changed, ` `        ``// then it is a prime ` `        ``if` `(prime[p] == ``false``)  ` `        ``{ ` ` `  `            ``// Update all multiples of p ` `            ``for` `(``int` `i = p * 2;  ` `                     ``i <= n; i += p) ` `                ``prime[i] = ``true``; ` `        ``} ` `    ``} ` `} ` ` `  `// function to print ` `// prime triplets ` `static` `void` `printPrimeTriplets(``int` `n) ` `{ ` `    ``// Finding all primes ` `    ``// from 1 to n ` `    ``bool``[] prime = ``new` `bool``[n + 1]; ` `    ``sieve(n, prime); ` `     `  `    ``Console.WriteLine(``"The prime triplets "` `+  ` `                               ``"from 1 to "` `+  ` `                               ``n + ``" are :"``); ` `     `  `    ``for` `(``int` `i = 2; i <= n - 6; ++i)  ` `    ``{ ` ` `  `        ``// triplets of form (p, p+2, p+6) ` `        ``if` `(!prime[i] &&  ` `            ``!prime[i + 2] &&  ` `            ``!prime[i + 6]) ` `            ``Console.WriteLine(i + ``" "` `+ (i + 2) +  ` `                                  ``" "` `+ (i + 6)); ` ` `  `        ``// triplets of form (p, p+4, p+6) ` `        ``else` `if` `(!prime[i] &&  ` `                 ``!prime[i + 4] &&  ` `                 ``!prime[i + 6]) ` `            ``Console.WriteLine(i + ``" "` `+ (i + 4) +  ` `                                  ``" "` `+ (i + 6)); ` `    ``} ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main() ` `{ ` `    ``int` `n = 25; ` `    ``printPrimeTriplets(n); ` `} ` `} ` ` `  `// This code is contributed by mits `

## PHP

 ` `

Output :

```The prime triplets from 1 to 25 are :
5 7 11
7 11 13
11 13 17
13 17 19
17 19 23
```

References :
Wiki

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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : Mithun Kumar

Article Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.