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) .
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
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.
The prime triplets from 1 to 25 are : 5 7 11 7 11 13 11 13 17 13 17 19 17 19 23
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.
- Generate a pythagoras triplet from a single integer
- Pythagorean Triplet with given sum
- Finding a Non Transitive Coprime Triplet in a Range
- Sum of multiplication of triplet of divisors of a number
- Triplet with no element divisible by 3 and sum N
- Count triplet pairs (A, B, C) of points in 2-D space that satisfy the given condition
- Find a triplet in an array whose sum is closest to a given number
- Find distinct integers for a triplet with given product
- Pythagorean Triplet with given sum using single loop
- Count triplet of indices (i, j, k) such that XOR of elements between [i, j) equals [j, k]
- Puzzle | Place numbers 1 to 9 in a Circle such that sum of every triplet in straight line is 15
- Largest lexicographic triplet from a given Array that forms a triangle
- Minimum possible sum of prices of a Triplet from the given Array
- Maximum value of expression (arr[i] + arr[j] * arr[k]) formed from a valid Triplet
- Check if every vertex triplet in graph contains two vertices connected to third vertex
- Quick ways to check for Prime and find next Prime in Java
- Find coordinates of a prime number in a Prime Spiral
- Prime numbers after prime P with sum S
- Sum of each element raised to (prime-1) % prime
- Print the nearest prime number formed by adding prime numbers to 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 firstname.lastname@example.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