Pares primos de Ormiston

Dados dos enteros N1 y N2 , la tarea es verificar si ambos pares son primos ormiston. 

Los números primos de Ormiston son aquellos números que son primos y tienen los mismos dígitos en diferente orden. 
Los primeros pares de primos de Ormiston son: 
(1913, 1931), (18379, 18397), (19013, 19031), (25013, 25031) ……etc. 
 

Ejemplos:  

Entrada: N1 = 1913, N2 = 1931 
Salida: SI

Entrada: n1 = 5, n2 = 7 
Salida: NO  

Planteamiento : La idea es comprobar que tanto N1 como N2 son números primos y anagrama o no el uno del otro. Si los números son primos y anagramas entre sí, entonces el número es pares primos de Ormiston.

A continuación se muestra la implementación del enfoque anterior: 

C++

// C++ implementation to
// check Ormiston prime
 
#include <iostream>
using namespace std;
 
// Function to check if the
// number is a 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;
}
 
const int TEN = 10;
 
// Function to update the frequency array
// such that freq[i] stores the
// frequency of digit i in n
void updateFreq(int n, int freq[])
{
 
    // While there are digits
    // left to process
    while (n) {
        int digit = n % TEN;
 
        // Update the frequency of
        // the current digit
        freq[digit]++;
 
        // Remove the last digit
        n /= TEN;
    }
}
 
// Function that returns true if a and b
// are anagrams of each other
bool areAnagrams(int a, int b)
{
 
    // To store the frequencies of
    // the digits in a and b
    int freqA[TEN] = { 0 };
    int freqB[TEN] = { 0 };
 
    // Update the frequency of
    // the digits in a
    updateFreq(a, freqA);
 
    // Update the frequency of
    // the digits in b
    updateFreq(b, freqB);
 
    // Match the frequencies of
    // the common digits
    for (int i = 0; i < TEN; i++) {
 
        // If frequency differs for any digit
        // then the numbers are not
        // anagrams of each other
        if (freqA[i] != freqB[i])
            return false;
    }
 
    return true;
}
 
// Returns true if n1 and
// n2 are Ormiston primes
bool OrmistonPrime(int n1, int n2)
{
    return (isPrime(n1) && isPrime(n2) &&
                 areAnagrams(n1, n2));
}
 
// Driver code
int main()
{
    int n1 = 1913, n2 = 1931;
    if (OrmistonPrime(n1, n2))
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
    return 0;
}

Java

// Java implementation to
// check Ormiston prime
import java.util.*;
class GFG{
  
// Function to check if the
// number is a 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;
}
  
static int TEN = 10;
  
// Function to update the frequency array
// such that freq[i] stores the
// frequency of digit i in n
static void updateFreq(int n, int freq[])
{
  
    // While there are digits
    // left to process
    while (n > 0)
    {
        int digit = n % TEN;
  
        // Update the frequency of
        // the current digit
        freq[digit]++;
  
        // Remove the last digit
        n /= TEN;
    }
}
  
// Function that returns true if a and b
// are anagrams of each other
static boolean areAnagrams(int a, int b)
{
  
    // To store the frequencies of
    // the digits in a and b
    int freqA[] = new int[TEN];
    int freqB[] = new int[TEN];
  
    // Update the frequency of
    // the digits in a
    updateFreq(a, freqA);
  
    // Update the frequency of
    // the digits in b
    updateFreq(b, freqB);
  
    // Match the frequencies of
    // the common digits
    for (int i = 0; i < TEN; i++)
    {
  
        // If frequency differs for any digit
        // then the numbers are not
        // anagrams of each other
        if (freqA[i] != freqB[i])
            return false;
    }
    return true;
}
  
// Returns true if n1 and
// n2 are Ormiston primes
static boolean OrmistonPrime(int n1, int n2)
{
    return (isPrime(n1) && isPrime(n2) &&
                   areAnagrams(n1, n2));
}
  
// Driver code
public static void main(String[] args)
{
    int n1 = 1913, n2 = 1931;
    if (OrmistonPrime(n1, n2))
        System.out.print("YES" +"\n");
    else
        System.out.print("NO" +"\n");
}
}
 
// This code is contributed by sapnasingh4991

Python3

# Python3 implementation to
# check Ormiston prime
 
# Function to check if the
# number is a 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
         
    i = 5
    while(i * i <= n):
        if (n % i == 0 or n % (i + 2) == 0):
            return False
             
        i = i + 6
     
    return True
 
TEN = 10
 
# Function to update the frequency array
# such that freq[i] stores the
# frequency of digit i in n
def updateFreq(n, freq):
 
    # While there are digits
    # left to process
    while (n):
        digit = n % TEN
 
        # Update the frequency of
        # the current digit
        freq[digit] += 1
 
        # Remove the last digit
        n = n // TEN
 
# Function that returns true if a and b
# are anagrams of each other
def areAnagrams(a, b):
     
    # To store the frequencies of
    # the digits in a and b
    freqA = [0] * TEN
    freqB = [0] * TEN 
 
    # Update the frequency of
    # the digits in a
    updateFreq(a, freqA)
     
    # Update the frequency of
    # the digits in b
    updateFreq(b, freqB)
 
    # Match the frequencies of
    # the common digits
    for i in range(TEN):
 
        # If frequency differs for any digit
        # then the numbers are not
        # anagrams of each other
        if (freqA[i] != freqB[i]):
            return False
 
    return True
 
# Returns true if n1 and
# n2 are Ormiston primes
def OrmistonPrime(n1, n2):
 
    return (isPrime(n1) and
            isPrime(n2) and
            areAnagrams(n1, n2))
             
# Driver code
n1, n2 = 1913, 1931
 
if (OrmistonPrime(n1, n2)):
    print("YES")
else:
    print("NO")
 
# This code is contributed by divyeshrabadiya07

C#

// C# implementation to
// check Ormiston prime
using System;
 
class GFG{
 
// Function to check if the
// number is a 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;
}
 
static int TEN = 10;
 
// Function to update the frequency array
// such that freq[i] stores the
// frequency of digit i in n
static void updateFreq(int n, int []freq)
{
     
    // While there are digits
    // left to process
    while (n > 0)
    {
        int digit = n % TEN;
 
        // Update the frequency of
        // the current digit
        freq[digit]++;
 
        // Remove the last digit
        n /= TEN;
    }
}
 
// Function that returns true if a and b
// are anagrams of each other
static bool areAnagrams(int a, int b)
{
 
    // To store the frequencies of
    // the digits in a and b
    int []freqA = new int[TEN];
    int []freqB = new int[TEN];
 
    // Update the frequency of
    // the digits in a
    updateFreq(a, freqA);
 
    // Update the frequency of
    // the digits in b
    updateFreq(b, freqB);
 
    // Match the frequencies of
    // the common digits
    for(int i = 0; i < TEN; i++)
    {
         
       // If frequency differs for any
       // digit then the numbers are not
       // anagrams of each other
       if (freqA[i] != freqB[i])
           return false;
    }
    return true;
}
 
// Returns true if n1 and
// n2 are Ormiston primes
static bool OrmistonPrime(int n1, int n2)
{
    return (isPrime(n1) && isPrime(n2) &&
                   areAnagrams(n1, n2));
}
 
// Driver code
public static void Main(String[] args)
{
    int n1 = 1913, n2 = 1931;
     
    if (OrmistonPrime(n1, n2))
        Console.Write("YES" + "\n");
    else
        Console.Write("NO" + "\n");
}
}
 
// This code is contributed by Princi Singh

Javascript

<script>
// Javascript implementation to
// check Ormiston prime
 
    // Function to check if the
    // number is a 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 (let i = 5; i * i <= n; i = i + 6)
            if (n % i == 0 || n % (i + 2) == 0)
                return false;
 
        return true;
    }
 
    const TEN = 10;
 
    // Function to update the frequency array
    // such that freq[i] stores the
    // frequency of digit i in n
    function updateFreq( n, freq)
    {
 
        // While there are digits
        // left to process
        while (n > 0) {
            let digit = n % TEN;
 
            // Update the frequency of
            // the current digit
            freq[digit]++;
 
            // Remove the last digit
            n = parseInt(n/TEN);
        }
    }
 
    // Function that returns true if a and b
    // are anagrams of each other
    function areAnagrams( a ,b) {
 
        // To store the frequencies of
        // the digits in a and b
        let freqA = Array(TEN).fill(0);
        let freqB = Array(TEN).fill(0);
 
        // Update the frequency of
        // the digits in a
        updateFreq(a, freqA);
 
        // Update the frequency of
        // the digits in b
        updateFreq(b, freqB);
 
        // Match the frequencies of
        // the common digits
        for ( i = 0; i < TEN; i++) {
 
            // If frequency differs for any digit
            // then the numbers are not
            // anagrams of each other
            if (freqA[i] != freqB[i])
                return false;
        }
        return true;
    }
 
    // Returns true if n1 and
    // n2 are Ormiston primes
    function OrmistonPrime( n1, n2)
    {
        return (isPrime(n1) && isPrime(n2) && areAnagrams(n1, n2));
    }
 
    // Driver code
    let n1 = 1913, n2 = 1931;
    if (OrmistonPrime(n1, n2))
        document.write("YES" + "\n");
    else
        document.write("NO" + "\n");
 
// This code is contributed by todaysgaurav
</script>
Producción: 

YES

 

Complejidad de tiempo: O(N)

Referencia: OEIS
 

Publicación traducida automáticamente

Artículo escrito por spp____ 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 *