📜  性感的总理

📅  最后修改于: 2021-04-27 19:12:07             🧑  作者: Mango

在数学中,性感素数是彼此相差六个的素数。例如,数字5和11都是性感素数,因为它们相差6。如果p + 2或p + 4(其中p是较低的素数)也是素数。

它们可以分为:

  • 性感素数对:它的形式为(p,p + 6),其中p和p + 6是素数。
    例如。 (11,17)是一个性感的素数对。
  • 性感素数三胞胎:素数(p,p + 6,p + 12)的三元组(使p + 18合成)称为性感素数三胞胎。
    例如。 (7、13、19)是性感的三胞胎。
  • 性感素数四联体:性感素数四联体(p,p + 6,p + 12,12,p + 18)只能以素数以十进制表示形式以1结尾(除了p = 5的四倍体除外)开始。
    例如。 (41、47、53、59)是一个性感素数四联体。
  • 性感素数五重奏:在五个具有共同差6的项的算术级数中,其中一个项必须被5整除,因为这两个数字是相对素数的。因此,唯一的性感素体五重奏是(5,11,17,23,29);性感素数的序列不再是可能的。

给定范围为[L,R]的形式。任务是打印该范围内的所有性感素数对。

例子:

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)

可以使用Eratosthenes筛网生成[L,R]范围内的Sexy Prime。这个想法是生成Sieve的布尔数组,并运行一个从L到R – 6(含)的i循环,并检查i和i + 6是否为质数。如果两者都是素数,请同时打印两个数字。

以下是此方法的实现:

C++
// CPP Program to print sexy prime in a range.
#include 
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


输出:

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