Sexy Prime

2

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


Output:

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

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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



2 Average Difficulty : 2/5.0
Based on 1 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.