In mathematics, Sexy Primes are prime numbers that differ from each other by six. For example, the numbers 5 and 11 are both sexy primes, because they differ by 6. If p + 2 or p + 4 (where p is the lower prime) is also prime.
They can be grouped as:
- Sexy prime pairs : It is of the form (p, p + 6), where p and p + 6 are prime numbers.
Eg. (11, 17) is a sexy prime pairs.
- Sexy prime triplets : Triplets of primes (p, p + 6, p + 12) such that p + 18 is composite are called sexy prime triplets.
Eg. (7, 13, 19) is a Sexy prime triplets.
- Sexy prime quadruplets : Sexy prime quadruplets (p, p + 6, p + 12, p + 18) can only begin with primes ending in a 1 in their decimal representation (except for the quadruplet with p = 5).
Eg. (41, 47, 53, 59) is a Sexy prime quadruplets.
- Sexy prime quintuplets : In an arithmetic progression of five terms with common difference 6, one of the terms must be divisible by 5, because the two numbers are relatively prime. Thus, the only sexy prime quintuplet is (5, 11, 17, 23, 29); no longer sequence of sexy primes is possible.
Given a range of the form [L, R].The task is to print all the sexy prime pairs in the range.
Input : L = 6, R = 59 Output : (7, 13) (11, 17) (13, 19) (17, 23) (23, 29) (31, 37) (37, 43) (41, 47) (47, 53) (53, 59) Input : L = 1, R = 19 Output : (5, 11) (7, 13) (11, 17) (13, 19)
Sexy Prime within a range [L, R] can be generated using Sieve Of Eratosthenes. The idea is to generate bool array of Sieve and run a loop of i from L to R – 6 (inclusive) and check whether i and i + 6 are prime or not. If both are prime, print both number.
Below is the implementation of this approach:
(7, 13) (11, 17) (13, 19) (17, 23) (23, 29) (31, 37) (37, 43) (41, 47) (47, 53) (53, 59)
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.
- Count Sexy Prime Pairs in the given array
- 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
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Check if a prime number can be expressed as sum of two Prime Numbers
- Check whether the sum of prime elements of the array is prime or not
- Print prime numbers with prime sum of digits in an array
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Sum of prime numbers without odd prime digits
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Check if a number is Prime, Semi-Prime or Composite for very large numbers
- Permutation of first N positive integers such that prime numbers are at prime indices
- Permutation of first N positive integers such that prime numbers are at prime indices | Set 2
- Count all prime numbers in a given range whose sum of digits is also prime