📜  双素数

📅  最后修改于: 2021-04-29 10:16:47             🧑  作者: Mango

双质数是那些质数,并且两个质数之间相差两(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个孪生素数。
除(3,5)外,每个双素数对都具有(6n – 1,1,6n + 1)的形式,其中包含自然数n。也就是说,两个素数之间的数字是6的倍数。
例子 :

Input : n1 = 11, n2 = 13
Output : Twin Prime

Input : n1 = 23, n2 = 37
Output : Not Twin Prime

先决条件:原始性测试|第一组(介绍和学校方法)

C++
// CPP program to check twin prime
#include 
using namespace std;
 
// Please refer below post for details of this
// function
// https://goo.gl/Wv3fGv
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;
  
    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;
  
    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;
  
    return true;
}
 
// Returns true if n1 and n2 are twin primes
bool twinPrime(int n1, int n2)
{
    return (isPrime(n1) && isPrime(n2) &&
                        abs(n1 - n2) == 2);
}
 
// Driver code
int main()
{
    int n1 = 11, n2 = 13;
    if (twinPrime(n1, n2))
        cout << "Twin Prime" << endl;
    else
        cout << endl
            << "Not Twin Prime" << endl;
    return 0;
}


Java
// JAVA Code for Twin Prime Numbers
import java.util.*;
 
class GFG {
 
    // Please refer below post for
    // details of this function
    // https://goo.gl/Wv3fGv
    static boolean isPrime(int n)
    {
        // Corner cases
        if (n <= 1) return false;
        if (n <= 3) return true;
 
        // This is checked so that we can skip
        // middle five numbers in below loop
        if (n % 2 == 0 || n % 3 == 0)
            return false;
 
        for (int i = 5; i * i <= n; i = i + 6)
            if (n % i == 0 || n % (i + 2) == 0)
                return false;
 
        return true;
    }
 
    // Returns true if n1 and n2 are twin primes
    static boolean twinPrime(int n1, int n2)
    {
        return (isPrime(n1) && isPrime(n2) &&
                     Math.abs(n1 - n2) == 2);
    }
 
    /* Driver program to test above function */
    public static void main(String[] args)
    {
        int n1 = 11, n2 = 13;
 
        if (twinPrime(n1, n2))
            System.out.println("Twin Prime");
        else
            System.out.println("Not Twin Prime");
    }
}
 
// This code is contributed by Arnav Kr. Mandal.


Python3
# Python3 code to check twin prime
import math
 
# Function to check whether a 
# number is prime or not
def isPrime( n ):
     
    # Corner cases
    if n <= 1:
        return False
    if n <= 3:
        return True
     
    # This is checked so that we
    # can skip middle five numbers
    # in below loop
    if n%2 == 0 or n%3 == 0:
        return False
     
    for i in range(5, int(math.sqrt(n)+1), 6):
        if n%i == 0 or n%(i + 2) == 0:
            return False
     
    return True
 
# Returns true if n1 and n2 are
# twin primes
def twinPrime(n1 , n2):
    return (isPrime(n1) and isPrime(n2) and
                    abs(n1 - n2) == 2)
 
# Driver code
n1 = 137
n2 = 139
 
if twinPrime(n1, n2):
    print("Twin Prime")
else:
    print("Not Twin Prime")
     
# This code is contributed by "Sharad_Bhardwaj".


C#
// C# Code for Twin Prime Numbers
using System;
 
class GFG {
 
    // Please refer below post for
    // details of this function
    // https://goo.gl/Wv3fGv
    static bool isPrime(int n)
    {
        // Corner cases
        if (n <= 1) return false;
        if (n <= 3) return true;
 
        // This is checked so that we can skip
        // middle five numbers in below loop
        if (n % 2 == 0 || n % 3 == 0)
            return false;
 
        for (int i = 5; i * i <= n; i = i + 6)
            if (n % i == 0 || n % (i + 2) == 0)
                return false;
 
        return true;
    }
 
    // Returns true if n1 and n2 are twin primes
    static bool twinPrime(int n1, int n2)
    {
        return (isPrime(n1) && isPrime(n2) &&
                    Math.Abs(n1 - n2) == 2);
    }
 
    // Driver program
    public static void Main()
    {
        int n1 = 11, n2 = 13;
 
        if (twinPrime(n1, n2))
            Console.WriteLine("Twin Prime");
        else
            Console.WriteLine("Not Twin Prime");
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出 :

Twin Prime