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++ program to implement // the above approach #include <bits/stdc++.h> using namespace std;
// Function to check if N // contains digits // 0, 1, 2, 5, 8 only bool checkDigits( int n)
{ // Extract digits of N
do {
int r = n % 10;
// Return false if any of
// these digits are present
if (r == 3 || r == 4 || r == 6
|| r == 7 || r == 9)
return false ;
n /= 10;
} while (n != 0);
return true ;
} // Function to check if // N is prime or not bool isPrime( int n)
{ if (n <= 1)
return false ;
// Check for all factors
for ( int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false ;
}
return true ;
} // Function to check if n is prime // in all the desired forms int isAllPrime( int n)
{ return isPrime(n)
&& checkDigits(n);
} // Driver Code int main()
{ int N = 101;
if (isAllPrime(N))
cout << "Yes" ;
else
cout << "No" ;
return 0;
} |
// Java program to implement // the above approach import java.util.*;
class GFG{
// Function to check if N // contains digits // 0, 1, 2, 5, 8 only static boolean checkDigits( int n)
{ // Extract digits of N
do
{
int r = n % 10 ;
// Return false if any of
// these digits are present
if (r == 3 || r == 4 ||
r == 6 || r == 7 || r == 9 )
return false ;
n /= 10 ;
} while (n != 0 );
return true ;
} // Function to check if // N is prime or not static boolean isPrime( int n)
{ if (n <= 1 )
return false ;
// Check for all factors
for ( int i = 2 ; i * i <= n; i++)
{
if (n % i == 0 )
return false ;
}
return true ;
} // Function to check if n is prime // in all the desired forms static boolean isAllPrime( int n)
{ return isPrime(n) &&
checkDigits(n);
} // Driver Code public static void main(String[] args)
{ int N = 101 ;
if (isAllPrime(N))
System.out.print( "Yes" );
else
System.out.print( "No" );
} } // This code is contributed by gauravrajput1 |
# Python3 program to implement # the above approach # Function to check if N # contains digits # 0, 1, 2, 5, 8 only def checkDigits(n):
# Extract digits of N
while True :
r = n % 10
# Return false if any of
# these digits are present
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
# Function to check if # N is prime or not def isPrime(n):
if (n < = 1 ):
return False
# Check for all factors
for i in range ( 2 , n + 1 ):
if i * i > n:
break
if (n % i = = 0 ):
return False
return True
# Function to check if n is prime # in all the desired forms def isAllPrime(n):
return isPrime(n) and checkDigits(n)
# Driver Code if __name__ = = '__main__' :
N = 101
if (isAllPrime(N)):
print ( "Yes" )
else :
print ( "No" )
# This code is contributed by mohit kumar 29 |
// C# program to implement // the above approach using System;
class GFG{
// Function to check if N // contains digits // 0, 1, 2, 5, 8 only static bool checkDigits( int n)
{ // Extract digits of N
do
{
int r = n % 10;
// Return false if any of
// these digits are present
if (r == 3 || r == 4 ||
r == 6 || r == 7 ||
r == 9)
return false ;
n /= 10;
} while (n != 0);
return true ;
} // Function to check if // N is prime or not static bool isPrime( int n)
{ if (n <= 1)
return false ;
// Check for all factors
for ( int i = 2; i * i <= n; i++)
{
if (n % i == 0)
return false ;
}
return true ;
} // Function to check if n is prime // in all the desired forms static bool isAllPrime( int n)
{ return isPrime(n) &&
checkDigits(n);
} // Driver Code public static void Main(String[] args)
{ int N = 101;
if (isAllPrime(N))
Console.Write( "Yes" );
else
Console.Write( "No" );
} } // This code is contributed by Rajput-Ji |
<script> // Javascript program to implement // the above approach // Function to check if N // contains digits // 0, 1, 2, 5, 8 only function checkDigits(n)
{ // Extract digits of N
do {
var r = n % 10;
// Return false if any of
// these digits are present
if (r == 3 || r == 4 || r == 6
|| r == 7 || r == 9)
return false ;
n = parseInt(n/10);
} while (n != 0);
return true ;
} // Function to check if // N is prime or not function isPrime(n)
{ if (n <= 1)
return false ;
// Check for all factors
for ( var i = 2; i * i <= n; i++) {
if (n % i == 0)
return false ;
}
return true ;
} // Function to check if n is prime // in all the desired forms function isAllPrime(n)
{ return isPrime(n)
&& checkDigits(n);
} // Driver Code var N = 101;
if (isAllPrime(N))
document.write( "Yes" );
else document.write( "No" );
</script> |
Yes
Time Complexity: O(N)
Auxiliary Space: O(1)