Sexy Prime

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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.
?>

chevron_right



Output:

(7, 13) (11, 17) (13, 19) (17, 23) 
(23, 29) (31, 37) (37, 43) (41, 47) 
(47, 53) (53, 59) 


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.