📜  检查数字是否为素数

📅  最后修改于: 2021-05-04 21:02:26             🧑  作者: Mango

给定正整数N ,任务是检查给定数字是否为质数。如果给定的数字很好,则打印’ YES ‘,否则打印’ NO ‘。

例子:

方法:

1.获取数字N。

2.初始化prev_prime = N-1和next_prime = N + 1

3.在prev_prime大于或等于2时迭代循环。并检查next_prime和prev_prime是否都是不使用素数的素数。

4.如果两者都不是素数,请重复步骤2和3。

5.如果next_prime和prev_prime都是素数,则检查N ^ 2> next_prime。 prev_prime与否。

  • 如果不是,则数字不是很好的素数,然后停止执行并返回NO。
  • 如果是,则重复步骤2、3、4和5。

以下是上述方法的实现:

C++
// C++ program to check if a number
// is good prime or not
#include
using namespace std;
 
// 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 loop
    if (n % 2 == 0 || n % 3 == 0)
        return false;
 
    for(int i = 5; i * i <= n; i += 6)
    {
       if (n % i == 0 || n % (i + 2) == 0)
           return false;
    }
    return true;
}
 
// Function to check if the
// given number is Good prime
bool isGoodprime (int n)
{
 
    // Smallest good prime is 5
    // So the number less than 5
    // can not be a Good prime
 
    if (n < 5)
        return false;
 
    int prev_prime = n - 1;
    int next_prime = n + 1;
 
    while (prev_prime >= 2)
    {
         
        // Calculate first prime number < n
        while (!isPrime(prev_prime))
        {
            prev_prime--;
        }
 
        // Calculate first prime number > n
        while (!isPrime(next_prime))
        {
            next_prime++;
        }
 
        // Check if product of next_prime
        // and prev_prime is less than n^2
        if ((prev_prime * next_prime) >= n * n)
            return false;
 
        prev_prime -= 1;
        next_prime += 1;
    }
    return true;
}
 
// Driver code
int main()
{
    int n = 11;
 
    if (isGoodprime(n))
        cout << "YES";
    else
        cout << "NO";
 
    return 0;
}
 
// This code is contributed by himanshu77


Java
// Java program to check if a number is
// good prime or not
class GFG{
 
// Function to check if a
// number is prime or not
static boolean 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 to check if the given
// number is good prime or not
static boolean isGoodrprime(int n)
{
 
    // Smallest good prime is 5
    // So the number less than 5
    // can not be a good prime
 
    if (n < 5)
        return false;
 
    int prev_prime = n - 1;
    int next_prime = n + 1;
 
    while (prev_prime >= 2)
    {
         
        // Calculate first prime number < n
        while (!isPrime(prev_prime))
        {
            prev_prime--;
        }
 
        // Calculate first prime number > n
        while (!isPrime(next_prime))
        {
            next_prime++;
        }
 
        // Check if product of next_prime
        // and prev_prime
        // is less than n^2
        if ((prev_prime * next_prime) >= n * n)
            return false;
 
        prev_prime -= 1;
        next_prime += 1;
    }
    return true;
}
 
// Driver code
public static void main(String []args)
{
    int n = 11;
     
    if (isGoodrprime(n))
        System.out.println("YES");
    else
        System.out.println("NO");
}
}
 
// This code is contributed by amal kumar choubey


Python3
# Python3 program to check if a number is
# good prime or not
 
# 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
 
    i = 5
    while (i * i <= n):
        if (n % i == 0 or n % (i + 2) == 0):
            return False
        i = i + 6
    return True
 
# Function to check if the given number
# is good prime or not
def isGoodrPrime(n):
 
    # Declaring variables as global
    global next_prime, prev_prime
 
    # Smallest good prime is 5
    # So the number less than 5
    # can not be a good prime
    if(n < 5):
        return False
 
    # Initialize previous_prime to n - 1
    # and next_prime to n + 1
    prev_prime = n - 1
    next_prime = n + 1
 
    while(prev_prime >= 2):
 
        # Calculate first prime number < n
        while (not isPrime(prev_prime)):
            prev_prime -= 1
 
        # Calculate first prime number > n
        while(not isPrime(next_prime)):
            next_prime += 1
 
        # Check if product of next_prime
        # and prev_prime
        # is less than n^2
        if((prev_prime * next_prime) >= n * n):
            return False
 
        prev_prime -= 1
        next_prime += 1
 
    return True
 
# Driver code
if __name__ == '__main__':
 
    n = 11
 
    if(isGoodrPrime(n)):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by Shivam Singh


C#
// C# program to check if a number is
// good prime  or not
 
using System;
class GFG {
 
    // Function to check if a
    // number is prime or not
    static 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 to check
    // if the given number is good prime or not
    static bool isGoodrprime(int n)
    {
 
        // Smallest good prime is 5
        // So the number less than 5
        // can not be a good prime
 
        if (n < 5)
            return false;
 
        int prev_prime = n - 1;
        int next_prime = n + 1;
 
        while (prev_prime >= 2) {
            // Calculate first prime number < n
            while (!isPrime(prev_prime)) {
                prev_prime--;
            }
 
            // Calculate first prime number > n
            while (!isPrime(next_prime)) {
                next_prime++;
            }
 
            // check if product of next_prime
            // and prev_prime
            // is less than n^2
 
            if ((prev_prime * next_prime)
                >= n * n)
                return false;
 
            prev_prime -= 1;
            next_prime += 1;
        }
        return true;
    }
 
    public static void Main()
    {
 
        int n = 11;
        if (isGoodrprime(n))
            Console.WriteLine("YES");
        else
            Console.WriteLine("NO");
    }
}


Javascript


输出:
YES

时间复杂度: O(n 3/2 )

辅助空间: O(1)