📜  打印所有数量少于其的Prime Quadruplet

📅  最后修改于: 2021-05-04 07:55:39             🧑  作者: Mango

给定正整数n,请在下面打印每个素数四元组n

素数四元组:在数学中,素数四元组是一组四个{ p,p + 2,p + 6,p + 8 }形式的素数。

例子 :

Input : N = 15
Output : 5  7  11  13
                           
Input : N = 20
Output :  5  7  11  13
           11 13 17  19

生成所有最多n个素数四联体的简单解决方案是遍历从i = 1到n的所有正整数’i’,并检查i,i + 2,i + 6和i + 8是否为素数。

一种有效的解决方案是使用Eratosthenes筛子来预先计算数组在一定范围内的所有质数。
方法

  1. 使用Eratosthenes筛子预先计算素数(请参阅)
  2. 从i = 0遍历到n-7,并检查i,i + 2,i + 6和i + 8是否也是素数。
  3. 如果是,则打印i,i + 2,i + 6,i + 8,
    否则增加i并再次检查

下面是上述想法的实现:

C++
// CPP Program to print prime quadruplet
  
#include 
using namespace std;
  
#define MAX 100000
  
bool prime[MAX];
  
// Utility function to generate prime numbers
void sieve()
{
    // Sieve Of Eratosthenes for generating
    // prime number.
    memset(prime, true, sizeof(prime));
  
    for (int p = 2; p * p < MAX; 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 < MAX; i += p)
                prime[i] = false;
        }
    }
}
  
// Function to print Prime quadruplet
void printPrimeQuad(int n)
{
  
    for (int i = 0; i < n - 7; i++) {
  
        if (prime[i] && prime[i + 2] && prime[i + 6]
            && prime[i + 8]) {
  
            cout << i << " " << i + 2 << " " << i + 6
                 << " " << i + 8 << "\n";
        }
    }
}
  
// Driver Code
int main()
{
    sieve();
    int n = 20;
  
    printPrimeQuad(20);
  
    return 0;
}


Java
// Java code to print prime Quarduplet in a range
import java.util.Arrays;
import java.util.Collections;
  
class GFG {
  
    static final int MAX = 1000000;
    static boolean[] prime = new boolean[MAX];
  
    // utility function to generate prime number
    public static void sieve()
    {
        // Sieve Of Eratosthenes for generating
        // prime number.
  
        // memset(prime, true, sizeof(prime));
        Arrays.fill(prime, true);
  
        for (int p = 2; p * p < MAX; 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 < MAX; i += p)
                    prime[i] = false;
            }
        }
    }
  
    // function to print prime Quadruplet
    static void printPrimeQuad(int n)
    {
        for (int i = 0; i < n - 7; i++) {
  
            if (prime[i] && prime[i + 2] && prime[i + 6]
                && prime[i + 8]) {
  
                System.out.println(i + " " + (i + 2) + " "
                                   + (i + 6) + " " + (i + 8));
            }
        }
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int n = 20;
  
        sieve();
  
        printPrimeQuad(n);
    }
}


Python3
# Python3 Program to print 
# prime quadruplet 
  
# from math lib import sqrt method
from math import sqrt
  
MAX = 100000
  
# Sieve Of Eratosthenes for generating 
# prime number.
prime = [True] * MAX
  
# Utility function to generate 
# prime numbers 
def sieve() :
  
    for p in range(2, int(sqrt(MAX)) + 1) :
  
        # 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 , MAX, p) :
                prime[i] = False
                  
      
# Function to print Prime quadruplet
def printPrimeQuad(n) :
  
    for i in range(n - 7) :
          
        if ( prime[i] and prime[i + 2] and prime[i + 6]
            and prime[i + 8]) :
  
            print(i,i + 2,i + 6,i + 8)
              
          
# Driver code
if __name__ == "__main__" :
      
    sieve()
    n = 20
  
    printPrimeQuad(20)
      
# This code is contributed by 
# ANKITRAI1


C#
// C# code to print prime Quarduplet in a range
  
using System;
  
class GFG {
   
     const int MAX = 1000000;
    static bool[] prime = new bool[MAX];
   
    // utility function to generate prime number
    public static void sieve()
    {
        // Sieve Of Eratosthenes for generating
        // prime number.
   
        // memset(prime, true, sizeof(prime));
        for(int i=0;i


PHP


输出:
5 7 11 13
11 13 17 19

也可以看看:

  • 双子星
  • 总理三胞胎
  • 性感的总理
  • 表亲Prime
  • 平衡素数