📜  奥米斯顿素数对

📅  最后修改于: 2021-04-27 05:41:06             🧑  作者: Mango

给定两个整数N1N2 ,任务是检查两个对是否都是ormiston素数。

例子:

方法:这个想法是要检查N1和N2两者是否都是素数和字谜。如果数字互为素数和字谜,则该数字为Ormiston素数对。

下面是上述方法的实现:

C++
// C++ implementation to
// check Ormiston prime
 
#include 
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


输出:
YES

参考: OEIS