Check if a number is prime in Flipped Upside Down, Mirror Flipped and Mirror Flipped Upside Down
Last Updated :
17 Mar, 2021
Given an integer N, the task is to check if N is a prime number in Flipped Down, Mirror Flipped and Mirror Flipped Down forms of the given number.
Examples:
Input: N = 120121
Output: Yes
Explanation:
Flipped forms of the number:
Flipped Upside Down – 151051
Mirror Flipped – 121021
Mirror Upside Down – 150151
Since 1510151 and 121021 are both prime numbers, the flipped numbers are prime.
Input: N = 12
Output: No
Explanation:
Flipped forms of the number:
Flipped Upside Down – 15
Mirror Flipped – 21
Mirror Upside Down – 51
All the flipped numbers are not prime.
Approach: Follow the steps below to solve the problem:
- Since the number N has to be prime in each of the Flipped upside down, Mirror Flipped and Mirror Upside Down forms, the only possible digits it should contain are {0, 1, 2, 5, 8}.
- Therefore, the problem reduces to check if the number is prime or not and if it is made up of the digits 0, 1, 2, 5, and 8 only.
- If found to be true, print “Yes“.
- Otherwise, print “No“.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool checkDigits( int n)
{
do {
int r = n % 10;
if (r == 3 || r == 4 || r == 6
|| r == 7 || r == 9)
return false ;
n /= 10;
} while (n != 0);
return true ;
}
bool isPrime( int n)
{
if (n <= 1)
return false ;
for ( int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false ;
}
return true ;
}
int isAllPrime( int n)
{
return isPrime(n)
&& checkDigits(n);
}
int main()
{
int N = 101;
if (isAllPrime(N))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean checkDigits( int n)
{
do
{
int r = n % 10 ;
if (r == 3 || r == 4 ||
r == 6 || r == 7 || r == 9 )
return false ;
n /= 10 ;
} while (n != 0 );
return true ;
}
static boolean isPrime( int n)
{
if (n <= 1 )
return false ;
for ( int i = 2 ; i * i <= n; i++)
{
if (n % i == 0 )
return false ;
}
return true ;
}
static boolean isAllPrime( int n)
{
return isPrime(n) &&
checkDigits(n);
}
public static void main(String[] args)
{
int N = 101 ;
if (isAllPrime(N))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def checkDigits(n):
while True :
r = n % 10
if (r = = 3 or r = = 4 or
r = = 6 or r = = 7 or
r = = 9 ):
return False
n / / = 10
if n = = 0 :
break
return True
def isPrime(n):
if (n < = 1 ):
return False
for i in range ( 2 , n + 1 ):
if i * i > n:
break
if (n % i = = 0 ):
return False
return True
def isAllPrime(n):
return isPrime(n) and checkDigits(n)
if __name__ = = '__main__' :
N = 101
if (isAllPrime(N)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool checkDigits( int n)
{
do
{
int r = n % 10;
if (r == 3 || r == 4 ||
r == 6 || r == 7 ||
r == 9)
return false ;
n /= 10;
} while (n != 0);
return true ;
}
static bool isPrime( int n)
{
if (n <= 1)
return false ;
for ( int i = 2; i * i <= n; i++)
{
if (n % i == 0)
return false ;
}
return true ;
}
static bool isAllPrime( int n)
{
return isPrime(n) &&
checkDigits(n);
}
public static void Main(String[] args)
{
int N = 101;
if (isAllPrime(N))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function checkDigits(n)
{
do {
var r = n % 10;
if (r == 3 || r == 4 || r == 6
|| r == 7 || r == 9)
return false ;
n = parseInt(n/10);
} while (n != 0);
return true ;
}
function isPrime(n)
{
if (n <= 1)
return false ;
for ( var i = 2; i * i <= n; i++) {
if (n % i == 0)
return false ;
}
return true ;
}
function isAllPrime(n)
{
return isPrime(n)
&& checkDigits(n);
}
var N = 101;
if (isAllPrime(N))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...