给定一个整数N ,任务是检查N是否是给定数字的翻转、镜像翻转和镜像翻转形式的素数。
例子:
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.
处理方法:按照以下步骤解决问题:
- 由于数字N在每个翻转倒置、镜像翻转和倒置镜像形式中都必须是素数,因此它应该包含的唯一可能数字是 {0, 1, 2, 5, 8}。
- 因此,该问题简化为检查数是素数或不且仅当它是由数字0,1,2,5,和8。
- 如果发现是真的,打印“是”。
- 否则,打印“否”。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
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
// 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
# 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#
// 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
Javascript
输出:
Yes
时间复杂度: O(N)
辅助空间: O(1)