📜  检查给定的数字是否是表兄弟素

📅  最后修改于: 2021-04-29 07:38:20             🧑  作者: Mango

给定两个正整数n1和n2,任务是检查两者是否都是表兄弟素数。如果两个数字都是表兄弟素,则打印“ YES”,否则打印“ NO”。
表亲素数:在数学中,表亲素数是相差4的素数。假设’p’是素数,如果(p + 4)也是素数,则两个素数都称为表亲素数。
低于100的表兄弟素数是:

例子:

Input: n1 = 7, n2 = 11
Output: YES
    Both are prime numbers and they differ by 4.

Input: n1 = 5, n2 = 11
Output: NO
    Both are prime numbers but they differ by 6.

方法:一种简单的解决方案是检查两个数字是否都是质数,相差是否为4。如果它们是素数并且相差4,那么它们将是表亲素数,否则不是。
下面是上述方法的实现:

CPP
// CPP program to check Cousin prime
 
#include 
using namespace std;
 
// Function to check if a number is prime or not
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 Cousin primes
bool isCousinPrime(int n1, int n2)
{
    // Check if they diifer by 4 or not
    if (abs(n1 - n2) != 4)
        return false;
 
    // Check if both are prime number or not
    else
        return (isPrime(n1) && isPrime(n2));
}
 
// Driver code
int main()
{
 
    // Get the 2 numbers
    int n1 = 7, n2 = 11;
 
    // Check the numbers for cousin prime
    if (isCousinPrime(n1, n2))
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
 
    return 0;
}


JAVA
// JAVA program to check Cousin prime
 
import java.util.*;
 
class GFG {
 
    // Function to check if a number is prime or not
    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 Cousin primes
    static boolean isCousinPrime(int n1, int n2)
    {
        // Check if they diifer by 4 or not
        if (Math.abs(n1 - n2) != 4)
            return false;
 
        // Check if both are prime number or not
        else
            return (isPrime(n1) && isPrime(n2));
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        // Get the 2 numbers
        int n1 = 7, n2 = 11;
 
        // Check the numbers for cousin prime
        if (isCousinPrime(n1, n2))
            System.out.println("YES");
        else
            System.out.println("NO");
    }
}


Python
# Python program to check Cousin 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 Cousin primes
def isCousinPrime( n1,  n2) :
 
    # Check if they diifer by 4 or not
    if(not (abs(n1-n2)== 4)):
        return False
     
    # Check if both are prime number or not
    else:
        return (isPrime(n1) and isPrime(n2))
  
 
# Driver code
 
# Get the 2 numbers
n1 = 7
n2 = 11
 
# Check the numbers for cousin prime
if (isCousinPrime(n1, n2)):
    print("YES")
else:
    print("NO")


C#
// C# Code for Cousin Prime Numbers
 
using System;
 
class GFG {
 
    // Function to check if the given
    // number is prime or not
    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 Cousin primes
    static bool isCousinPrime(int n1, int n2)
    {
        // Check if the numbers differ by 4 or not
        if (Math.Abs(n1 - n2) != 4) {
            return false;
        }
        else {
            return (isPrime(n1) && isPrime(n2));
        }
    }
 
    // Driver program
    public static void Main()
    {
 
        // Get the 2 numbers
        int n1 = 7, n2 = 11;
 
        // Check the numbers for cousin prime
        if (isCousinPrime(n1, n2))
            Console.WriteLine("YES");
        else
            Console.WriteLine("NO");
    }
}


PHP


输出:
YES

时间复杂度: O(n1 1/2 )

辅助空间: O(1)