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
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Print the nearest prime number formed by adding prime numbers to N
- Sum of multiplication of triplet of divisors of a number
- Quick ways to check for Prime and find next Prime in Java
- Finding a Non Transitive Coprime Triplet in a Range
- Generate a pythagoras triplet from a single integer
- Find a triplet in an array whose sum is closest to a given number
- 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
- 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
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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