📜  1至n之间的双素数

📅  最后修改于: 2021-05-04 15:45:31             🧑  作者: Mango

给定整数n。我们需要打印1至n之间的所有双素数对。双质数是那些质数,并且两个质数之间相差两(2)的那些数。换句话说,孪生素数是素数差距为2的素数。
有时,术语“双生质”用于一对双生质。此的另一个名称是素生双胞胎或素对。通常,对(2,3)不被视为一对孪生素数。由于2是唯一的偶数质数,因此这对是唯一相差一个的质数对;因此,孪生素数与其他两个素数的间隔尽可能近。
前几个孪生素数对是:

(3, 5), (5, 7), (11, 13), (17, 19), (29, 31), 
(41, 43), (59, 61), (71, 73), (101, 103), 
(107, 109), (137, 139), …etc.

事实:10,000以下有409个孪生素数。
例子:

Input : n = 15
Output : (3, 5), (5, 7), (11, 13)

Input : 25
Output :(3, 5), (5, 7), (11, 13), (17, 19)

方法:
使用Eratosthenes筛子,找到所有小于或等于n的质数的列表,然后再次迭代列表直到n,然后检查第i数字,如果它们均为(i + 2)均是质数,则检查其(i + 2)数字,然后再打印两个数字继续至下一个数字以查找Twin prime。

C++
// C++ program print all twin primes
// using Sieve of Eratosthenes.
#include 
using namespace std;
 
void printTwinPrime(int n)
{
    // Create a boolean array "prime[0..n]"
    // and initialize all entries it as true.
    // A value in prime[i] will finally be false
    // if i is Not a prime, else true.
    bool prime[n + 1];
    memset(prime, true, sizeof(prime));
 
    for (int p = 2; p * p <= n; 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 <= n; i += p)
                prime[i] = false;
        }
    }
 
    // to check for twin prime numbers
    // display the twin primes
    for (int p = 2; p <= n - 2; p++)
        if (prime[p] && prime[p + 2])
            cout << "(" << p << ", " << p + 2 << ")" ;
}
 
// Driver Program to test above function
int main()
{
    int n = 25;
     
    // Calling the function
    printTwinPrime(n);
 
    return 0;
}


Java
// Java program to print all Twin Prime
// Numbers using Sieve of Eratosthenes
import java.io.*;
 
class GFG {
 
    static void printTwinPrime(int n)
    {
        // Create a boolean array "prime[0..n]"
        // and initialize all entries it as
        // true. A value in prime[i] will
        // finally be false if i is Not a
        // prime, else true.
        boolean prime[] = new boolean[n + 1];
         
        for (int i = 0; i <= n; i++)
            prime[i] = true;
 
        for (int p = 2; p * p <= n; 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 <= n; i += p)
                    prime[i] = false;
            }
        }
 
        // to check for twin prime numbers
        // display th twin prime
        for (int i = 2; i <= n - 2; i++) {
 
            if (prime[i] == true &&
                prime[i + 2] == true)
             
                // Display the result
                System.out.print(" (" + i + ", " +
                                   (i + 2) + ")");
        }
    }
 
    // Driver Program to test above function
    public static void main(String args[])
    {
        int n = 25;
        printTwinPrime(n);
    }
}


Python
# Python program to illustrate...
# To print total number of twin prime
# using Sieve of Eratosthenes
 
def printTwinPrime(n):
     
    # Create a boolean array "prime[0..n]"
    # and initialize all entries it as
    # true. A value in prime[i] will
    # finally be false if i is Not a prime,
    # else true.
    prime = [True for i in range(n + 2)]
    p = 2
     
    while (p * p <= n + 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, n + 2, p):
                prime[i] = False
        p += 1
     
    # check twin prime numbers
    # display the twin prime numbers
    for p in range(2, n-1):
        if prime[p] and prime[p + 2]:
            print("(",p,",", (p + 2), ")" ,end='')
 
 
# driver program
if __name__=='__main__':
     
    # static input
    n = 25
     
    # Calling the function
    printTwinPrime(n)


C#
// C# program to illustrate..
// print all twin primes
// Using Sieve of Eratosthenes
using System;
 
public class GFG {
 
    public static void printtwinprime(int n)
    {
 
        // Create a boolean array "prime[0..n]"
        // and initialize all entries it as
        // true. A value in prime[i] will
        // finally be false if i is Not a
        // prime, else true.
        bool[] prime = new bool[n + 1];
 
        for (int i = 0; i < n + 1; i++)
            prime[i] = true;
 
        for (int p = 2; p * p <= n; 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 <= n; i += p)
                    prime[i] = false;
            }
        }
 
        // check for twin prime numbers
        // To display th result
        for (int i = 2; i <= n - 2; i++) {
            if (prime[i] == true && prime[i + 2] == true)
                Console.Write(" (" + i + ", " + (i + 2) + ")");
        }
    }
 
    // Driver Code
    public static void Main()
    {
        // static input
        int n = 25;
         
 
        // calling the function
        printtwinprime(n);
    }  
}


PHP


Javascript


输出:
(3, 5)(5, 7)(11, 13)(17, 19)