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.
Examples:
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:
C++
// CPP Program to print sexy prime in a range. #include <bits/stdc++.h> using namespace std; // Print the sexy prime in a range void sexyprime( int l, int r) { // Sieve Of Eratosthenes for generating // prime number. bool prime[r + 1]; memset (prime, true , sizeof (prime)); for ( int p = 2; p * p <= r; 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 <= r; i += p) prime[i] = false ; } } // From L to R - 6, checking if i, // i + 6 are prime or not. for ( int i = l; i <= r - 6; i++) if (prime[i] && prime[i + 6]) cout << "(" << i << ", " << i + 6 << ") " ; } // Driven Program int main() { int L = 6, R = 59; sexyprime(L, R); return 0; } |
Java
// Java code to print sexy prime in a range. import java.util.Arrays; import java.util.Collections; class GFG { // Print the sexy prime in a range public static void sexyprime( int l, int r) { // Sieve Of Eratosthenes for generating // prime number. boolean [] prime= new boolean [r + 1 ]; // memset(prime, true, sizeof(prime)); Arrays.fill(prime, true ); for ( int p = 2 ; p * p <= r; 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 <= r; i += p) prime[i] = false ; } } // From L to R - 6, checking if i, // i + 6 are prime or not. for ( int i = l; i <= r - 6 ; i++) if (prime[i] && prime[i + 6 ]) System.out.print( "(" + i + ", " + (i + 6 ) + ") " ); } // Driver program to test above methods public static void main(String[] args) { int L = 6 , R = 59 ; sexyprime(L, R); } } // This code is contributed by Chhavi |
Python 3
# Python 3 Program to print # sexy prime in a range. # Print the sexy prime in a range def sexyprime(l, r) : # Sieve Of Eratosthenes # for generating # prime number. prime = [ True ] * (r + 1 ) p = 2 while (p * p < = r) : # If prime[p] is not changed, # then it is a prime if (prime[p] = = True ) : # Update all multiples of p for i in range ( p * 2 , r + 1 ,p) : prime[i] = False p = p + 1 # From L to R - 6, checking if i, # i + 6 are prime or not. for i in range ( l,r - 6 + 1 ) : if (prime[i] and prime[i + 6 ]) : print ( "(" , i , "," , i + 6 , ")" , end = "") # Driven Program L = 6 R = 59 sexyprime(L, R) # This code is contributed by Nikita Tiwari. |
C#
// C# code to print sexy // prime in a range. using System; class GFG { // Print the sexy // prime in a range public static void sexyprime( int l, int r) { // Sieve Of Eratosthenes // for generating prime number. int [] prime = new int [r + 1]; // memset(prime, true, // sizeof(prime)); for ( int i = 0; i < r + 1; i++) prime[i] = 1; for ( int p = 2; p * p <= r; p++) { // If prime[p] is not changed, // then it is a prime if (prime[p] == 1) { // Update all multiples of p for ( int i = p * 2; i <= r; i += p) prime[i] = 0; } } // From L to R - 6, checking // if i, i + 6 are prime or not. for ( int i = l; i <= r - 6; i++) if (prime[i] == 1 && prime[i + 6] == 1) Console.Write( "(" + i + ", " + (i + 6) + ") " ); } // Driver Code public static void Main() { int L = 6, R = 59; sexyprime(L, R); } } // This code is contributed by mits |
PHP
<?php // PHP Program to print // sexy prime in a range. // Print the sexy // prime in a range function sexyprime( $l , $r ) { // Sieve Of Eratosthenes for // generating prime number. $prime = array_fill (0, $r + 1, true); for ( $p = 2; $p * $p <= $r ; $p ++) { // If prime[p] is not // changed, then it // is a prime if ( $prime [ $p ] == true) { // Update all // multiples of p for ( $i = $p * 2; $i <= $r ; $i += $p ) $prime [ $i ] = false; } } // From L to R - 6, // checking if i, // i + 6 are prime or not. for ( $i = $l ; $i <= $r - 6; $i ++) if ( $prime [ $i ] && $prime [ $i + 6]) echo "(" , $i , ", " , $i + 6 , ") " ; } // Driver Code $L = 6; $R = 59; sexyprime( $L , $R ); // This code is contributed // by ajit. ?> |
Javascript
<script> // Javascript Program to print sexy prime in a range. // Print the sexy prime in a range function sexyprime(l, r) { // Sieve Of Eratosthenes for generating // prime number. var prime = Array(r+1).fill( true ); for ( var p = 2; p * p <= r; p++) { // If prime[p] is not changed, // then it is a prime if (prime[p] == true ) { // Update all multiples of p for ( var i = p * 2; i <= r; i += p) prime[i] = false ; } } // From L to R - 6, checking if i, // i + 6 are prime or not. for ( var i = l; i <= r - 6; i++) if (prime[i] && prime[i + 6]) document.write( "(" + i + ", " + (i + 6) + ") " ); } // Driven Program var L = 6, R = 59; sexyprime(L, R); </script> |
Output:
(7, 13) (11, 17) (13, 19) (17, 23) (23, 29) (31, 37) (37, 43) (41, 47) (47, 53) (53, 59)
Time Complexity: O(R*log(log(R)))
Auxiliary Space: O(R)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!