📜  检查“翻转倒置”,“反光镜倒置”和“反光镜倒置”中的数字是否为质数

📅  最后修改于: 2021-04-29 03:00:37             🧑  作者: Mango

给定整数N ,任务是检查N是否为给定数字的Flipped Down,Mirror Flipped和Mirror Flipped Down形式的质数。
例子:

方法:请按照以下步骤解决问题:

  1. 由于在翻转的上下翻转,镜像翻转的和翻转上下的表单中,数字N必须是素数,因此它应仅包含{0,1,2,5,8}。
  2. 因此,该问题简化为检查数是素数或不且仅当它是由数字0,1,2,5,8。
  3. 如果发现是真的,则打印“”。
  4. 否则,打印“ No ”。

下面是上述方法的实现:

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)