Open In App

Ormiston prime Pairs

Last Updated : 13 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Given two integer N1 and N2, the task is to check if both the pairs are ormiston prime. 

Ormiston prime is those numbers that are prime and have the same digits in a different order. 
The first few Ormiston prime pairs are: 
(1913, 1931), (18379, 18397), (19013, 19031), (25013, 25031) ……etc. 
 

Examples:  

Input: N1 = 1913, N2 = 1931 
Output: YES

Input: n1 = 5, n2 = 7 
Output: NO  

Approach: The idea is to check that both and N1 and N2 are primes and anagram or not of each other. If the numbers are prime and anagram of each other then the number is Ormiston prime pairs.

Below is the implementation of the above approach: 

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>


Output: 

YES

 

Time Complexity: O(N)

Reference: OEIS
 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads