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
- Pythagorean Triplet with given sum
- Triplet with no element divisible by 3 and sum N
- Print the nearest prime number formed by adding prime numbers to N
- Quick ways to check for Prime and find next Prime in Java
- Check if a prime number can be expressed as sum of two Prime Numbers
- Find coordinates of a prime number in a Prime Spiral
- Print prime numbers with prime sum of digits in an array
- Finding a Non Transitive Coprime Triplet in a Range
- Generate a pythagoras triplet from a single integer
- Check whether the sum of prime elements of the array is prime or not
- Count triplet pairs (A, B, C) of points in 2-D space that satisfy the given condition
- Sum of each element raised to (prime-1) % prime
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Prime numbers after prime P with sum S
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