Comprueba si los números dados son primos primos o no

Dados dos enteros positivos n1 y n2, la tarea es verificar si ambos son primos primos o no. Imprime ‘SÍ’ si ambos números son primos primos; de lo contrario, imprime ‘NO’.
Primos primos: en matemáticas, los primos primos son números primos que difieren en 4. Supongamos que ‘p’ es un número primo y si (p + 4) también es un número primo, ambos números primos se llamarán primos primos. 
Los primos primos por debajo de 100 son: 
 

(3, 7), (7, 11), (13, 17), (19, 23), (37, 41), (43, 47), (67, 71), (79, 83), (97 , 101)

Ejemplos:  

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.

Enfoque: una solución simple es verificar si ambos números son primos y difieren en 4 o no. Si son primos y difieren en 4 entonces serán primos primos, de lo contrario no.
A continuación se muestra la implementación del enfoque anterior: 

CPP

// CPP program to check Cousin prime
 
#include <bits/stdc++.h>
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 differ 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 differ 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 differ 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

<?php
// PhP program to check Cousin prime
 
 
// Function to check if the given
// Number is prime or not
function 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 || $n % 3 == 0)
        return false;
 
    for ($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
function isCousinPrime($n1, $n2)
{
    // Check if the given number differ by 4 or not
    if(abs($n1-$n2)!=4)
        return false;
     
    // Check if both numbers are prime or not
    else
        return (isPrime($n1) && isPrime($n2));
}
 
// Driver code
 
// Get the 2 numbers
$n1 = 7;
$n2 = 11;
 
    // Check the numbers for cousin prime
if (isCousinPrime($n1, $n2))
    echo "YES";
else
    echo "NO";
 
?>

Javascript

<script>
 
// Javascript program to check Cousin prime
 
// Function to check if a number is prime or not
function 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 || n % 3 == 0)
        return false;
 
    for (var 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
function isCousinPrime(n1, n2)
{
    // Check if they differ 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
// Get the 2 numbers
var n1 = 7, n2 = 11;
// Check the numbers for cousin prime
if (isCousinPrime(n1, n2))
    document.write( "YES");
else
    document.write( "NO" );
 
</script>
Producción: 

YES

 

Complejidad de tiempo: O(n1 1/2 )

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por ihritik y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *