📜  检查N是否为阶乘素数

📅  最后修改于: 2021-05-05 02:49:18             🧑  作者: Mango

给定正整数N ,任务是检查N是否为阶乘素数。如果它是阶乘素数,则打印YES,否则打印NO

注意:在数学中,阶乘质数是小于任何数阶乘的质数。前几个阶乘素数是2、3、5、7、23、719、5039等

例子:

方法:为了使N为阶乘数, N必须为质数,并且N – 1N +1应为任意数的阶乘值。

  • 如果N不是素数,则打印No。
  • 否则设置事实= 1并从i = 1开始更新事实=事实* i ,如果事实= N – 1事实= N + 1,则打印
  • 重复上述步骤,直到事实≤N +1为止,如果不满足条件,则最后打印No。

下面是上述方法的实现:

C++
// C++ program to check if given
// number is a factorial prime
  
#include 
using namespace std;
  
// Utility function to check
// if a number is prime or not
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
  
    // This is checked so that we can skip
    // middle five numbers in below loop
    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;
}
  
// Function that returns true if n is a factorial prime
bool isFactorialPrime(long n)
{
  
    // If n is not prime then return false
    if (!isPrime(n))
        return false;
  
    long fact = 1;
    int i = 1;
    while (fact <= n + 1) {
  
        // Calculate factorial
        fact = fact * i;
  
        // If n is a factorial prime
        if (n + 1 == fact || n - 1 == fact)
            return true;
  
        i++;
    }
  
    // n is not a factorial prime
    return false;
}
  
// Driver code
int main()
{
  
    int n = 23;
  
    if (isFactorialPrime(n))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}


Java
// Java program to check if given
// number is a factorial prime
class GFG {
  
    // Utility function to check
    // if a number is prime or not
    static boolean isPrime(long n)
    {
        // Corner cases
        if (n <= 1)
            return false;
        if (n <= 3)
            return true;
  
        // This is checked so that we can skip
        // middle five numbers in below loop
        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;
    }
  
    // Function that returns true if n is a factorial prime
    static boolean isFactorialPrime(long n)
    {
  
        // If n is not prime then return false
        if (!isPrime(n))
            return false;
  
        long fact = 1;
        int i = 1;
        while (fact <= n + 1) {
  
            // Calculate factorial
            fact = fact * i;
  
            // If n is a factorial prime
            if (n + 1 == fact || n - 1 == fact)
                return true;
  
            i++;
        }
  
        // n is not a factorial prime
        return false;
    }
  
    // Driver code
    public static void main(String args[])
    {
  
        int n = 23;
  
        if (isFactorialPrime(n))
            System.out.println("Yes");
  
        else
            System.out.println("No");
    }
}


Python3
# Python3 program to check if given 
# number is a factorial prime 
  
# from math lib import sqrt function
from math import sqrt
  
# Utility function to check 
# if a number is prime or not 
def isPrime(n) : 
      
    # Corner cases 
    if (n <= 1) :
        return False
          
    if (n <= 3) :
        return True
  
    # This is checked so that we can skip 
    # middle five numbers in below loop 
    if (n % 2 == 0 or n % 3 == 0) :
        return False
  
    for i in range(5, int(sqrt(n)) + 1, 6) :
        if (n % i == 0 or n % (i + 2) == 0) :
            return False
  
    return True
  
# Function that returns true if n 
# is a factorial prime 
def isFactorialPrime(n) : 
  
    # If n is not prime then return false 
    if (not isPrime(n)) :
        return False
      
    fact = 1
    i = 1
    while (fact <= n + 1) :
  
        # Calculate factorial 
        fact = fact * i 
  
        # If n is a factorial prime 
        if (n + 1 == fact or n - 1 == fact) : 
            return True
  
        i += 1
  
    # n is not a factorial prime 
    return False
  
# Driver code 
if __name__ == "__main__" : 
  
    n = 23
  
    if (isFactorialPrime(n)) :
        print("Yes")
    else :
        print("No")
  
# This code is contributed by Ryuga


C#
// C# program to check if given
// number is a factorial prime
using System;
class GFG {
  
    // Utility function to check
    // if a number is prime or not
    static bool isPrime(long n)
    {
        // Corner cases
        if (n <= 1)
            return false;
        if (n <= 3)
            return true;
  
        // This is checked so that we can skip
        // middle five numbers in below loop
        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;
    }
  
    // Function that returns true if n is a factorial prime
    static bool isFactorialPrime(long n)
    {
  
        // If n is not prime then return false
        if (!isPrime(n))
            return false;
  
        long fact = 1;
        int i = 1;
        while (fact <= n + 1) {
  
            // Calculate factorial
            fact = fact * i;
  
            // If n is a factorial prime
            if (n + 1 == fact || n - 1 == fact)
                return true;
  
            i++;
        }
  
        // n is not a factorial prime
        return false;
    }
  
    // Driver code
    public static void Main()
    {
  
        int n = 23;
  
        if (isFactorialPrime(n))
            Console.WriteLine("Yes");
  
        else
            Console.WriteLine("No");
    }
}


PHP


输出:
Yes