Ormiston prime Pairs
Last Updated :
13 Jul, 2021
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++
#include <iostream>
using namespace std;
bool isPrime( int n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
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;
void updateFreq( int n, int freq[])
{
while (n) {
int digit = n % TEN;
freq[digit]++;
n /= TEN;
}
}
bool areAnagrams( int a, int b)
{
int freqA[TEN] = { 0 };
int freqB[TEN] = { 0 };
updateFreq(a, freqA);
updateFreq(b, freqB);
for ( int i = 0; i < TEN; i++) {
if (freqA[i] != freqB[i])
return false ;
}
return true ;
}
bool OrmistonPrime( int n1, int n2)
{
return (isPrime(n1) && isPrime(n2) &&
areAnagrams(n1, n2));
}
int main()
{
int n1 = 1913, n2 = 1931;
if (OrmistonPrime(n1, n2))
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean isPrime( int n)
{
if (n <= 1 )
return false ;
if (n <= 3 )
return true ;
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 ;
static void updateFreq( int n, int freq[])
{
while (n > 0 )
{
int digit = n % TEN;
freq[digit]++;
n /= TEN;
}
}
static boolean areAnagrams( int a, int b)
{
int freqA[] = new int [TEN];
int freqB[] = new int [TEN];
updateFreq(a, freqA);
updateFreq(b, freqB);
for ( int i = 0 ; i < TEN; i++)
{
if (freqA[i] != freqB[i])
return false ;
}
return true ;
}
static boolean OrmistonPrime( int n1, int n2)
{
return (isPrime(n1) && isPrime(n2) &&
areAnagrams(n1, n2));
}
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" );
}
}
|
Python3
def isPrime(n):
if (n < = 1 ):
return False
if (n < = 3 ):
return True
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
def updateFreq(n, freq):
while (n):
digit = n % TEN
freq[digit] + = 1
n = n / / TEN
def areAnagrams(a, b):
freqA = [ 0 ] * TEN
freqB = [ 0 ] * TEN
updateFreq(a, freqA)
updateFreq(b, freqB)
for i in range (TEN):
if (freqA[i] ! = freqB[i]):
return False
return True
def OrmistonPrime(n1, n2):
return (isPrime(n1) and
isPrime(n2) and
areAnagrams(n1, n2))
n1, n2 = 1913 , 1931
if (OrmistonPrime(n1, n2)):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG{
static bool isPrime( int n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
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;
static void updateFreq( int n, int []freq)
{
while (n > 0)
{
int digit = n % TEN;
freq[digit]++;
n /= TEN;
}
}
static bool areAnagrams( int a, int b)
{
int []freqA = new int [TEN];
int []freqB = new int [TEN];
updateFreq(a, freqA);
updateFreq(b, freqB);
for ( int i = 0; i < TEN; i++)
{
if (freqA[i] != freqB[i])
return false ;
}
return true ;
}
static bool OrmistonPrime( int n1, int n2)
{
return (isPrime(n1) && isPrime(n2) &&
areAnagrams(n1, n2));
}
public static void Main(String[] args)
{
int n1 = 1913, n2 = 1931;
if (OrmistonPrime(n1, n2))
Console.Write( "YES" + "\n" );
else
Console.Write( "NO" + "\n" );
}
}
|
Javascript
<script>
function isPrime( n) {
if (n <= 1)
return false ;
if (n <= 3)
return true ;
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 updateFreq( n, freq)
{
while (n > 0) {
let digit = n % TEN;
freq[digit]++;
n = parseInt(n/TEN);
}
}
function areAnagrams( a ,b) {
let freqA = Array(TEN).fill(0);
let freqB = Array(TEN).fill(0);
updateFreq(a, freqA);
updateFreq(b, freqB);
for ( i = 0; i < TEN; i++) {
if (freqA[i] != freqB[i])
return false ;
}
return true ;
}
function OrmistonPrime( n1, n2)
{
return (isPrime(n1) && isPrime(n2) && areAnagrams(n1, n2));
}
let n1 = 1913, n2 = 1931;
if (OrmistonPrime(n1, n2))
document.write( "YES" + "\n" );
else
document.write( "NO" + "\n" );
</script>
|
Time Complexity: O(N)
Reference: OEIS
Share your thoughts in the comments
Please Login to comment...