📜  检查数字是否为Primorial Prime

📅  最后修改于: 2021-05-04 08:06:14             🧑  作者: Mango

给定正数N,任务是检查N是否为原始质数。如果N是原始质数,则打印“ YES”,否则打印“ NO”。

素数素数在数学中,素数素数是形式为p n #+ 1p n #– 1的素数,其中p np n的素数,即前n个素数的乘积。

例子

Input : N = 5
Output : YES
5 is Primorial prime of the form pn - 1  
for n=2, Primorial is 2*3 = 6
and 6-1 =5.

Input : N = 31
Output : YES
31 is Primorial prime of the form pn + 1  
for n=3, Primorial is 2*3*5 = 30
and 30+1 = 31.

前几个主要素数是:

先决条件:

  • 原始的
  • Eratosthenes筛

方法:

  1. 使用Eratosthenes筛网生成该范围内的所有素数。
  2. 检查n是否为质数,如果n不是质数,则打印否
  3. 否则,从第一个质数(即2)开始乘以下一个质数,并继续检查乘积+ 1 = n或乘积– 1 = n
  4. 如果product + 1 = nproduct-1 = n ,则n是基本素,否则不是。

下面是上述方法的实现:

C++
// CPP program to check Primorial Prime
  
#include 
using namespace std;
  
#define MAX 10000
  
vector arr;
  
bool prime[MAX];
  
// Function to generate prime numbers
void SieveOfEratosthenes()
{
    // Create a boolean array "prime[0..n]" and initialize
    // make all entries of boolean array 'prime'
    // as true. A value in prime[i] will
    // finally be false if i is Not a prime, else true.
  
    memset(prime, true, sizeof(prime));
  
    for (int p = 2; p * p < MAX; p++) {
        // If prime[p] is not changed, then it is a prime
  
        if (prime[p] == true) {
  
            // Update all multiples of p
            for (int i = p * 2; i < MAX; i += p)
                prime[i] = false;
        }
    }
  
    // store all prime numbers
    // to vector 'arr'
    for (int p = 2; p < MAX; p++)
        if (prime[p])
            arr.push_back(p);
}
  
// Function to check the number for Primorial prime
bool isPrimorialPrime(long n)
{
    // If n is not prime Number
    // return flase
    if (!prime[n])
        return false;
  
    long long product = 1;
    int i = 0;
  
    while (product < n) {
  
        // Multiply next prime number
        // and check if product + 1 = n or Product-1 =n
        // holds or not
        product = product * arr[i];
  
        if (product + 1 == n || product - 1 == n)
            return true;
  
        i++;
    }
  
    return false;
}
  
// Driver code
int main()
{
    SieveOfEratosthenes();
  
    long n = 31;
  
    // Check if n is Primorial Prime
    if (isPrimorialPrime(n))
        cout << "YES\n";
    else
        cout << "NO\n";
  
    return 0;
}


Java
// Java program to check Primorial prime
  
import java.util.*;
  
class GFG {
  
    static final int MAX = 1000000;
    static Vector arr = new Vector();
    static boolean[] prime = new boolean[MAX];
  
    // Function to get the prime numbers
    static void SieveOfEratosthenes()
    {
  
        // make all entries of boolean array 'prime'
        // as true. A value in prime[i] will
        // finally be false if i is Not a prime, else true.
  
        for (int i = 0; i < MAX; i++)
            prime[i] = true;
  
        for (int p = 2; p * p < MAX; p++) {
  
            // If prime[p] is not changed, then it is a prime
            if (prime[p] == true) {
  
                // Update all multiples of p
                for (int i = p * 2; i < MAX; i += p)
                    prime[i] = false;
            }
        }
  
        // store all prime numbers
        // to vector 'arr'
        for (int p = 2; p < MAX; p++)
            if (prime[p])
                arr.add(p);
    }
  
    // Function to check the number for Primorial prime
    static boolean isPrimorialPrime(int n)
    {
        // If n is not prime
        // Then return false
        if (!prime[n])
            return false;
  
        long product = 1;
        int i = 0;
        while (product < n) {
  
            // Multiply next prime number
            // and check if product + 1 = n or product -1=n
            // holds or not
            product = product * arr.get(i);
  
            if (product + 1 == n || product - 1 == n)
                return true;
  
            i++;
        }
  
        return false;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        SieveOfEratosthenes();
  
        int n = 31;
  
        if (isPrimorialPrime(n))
            System.out.println("YES");
        else
            System.out.println("NO");
    }
}


Python 3
# Python3 Program to check Primorial Prime 
  
# from math lib import sqrt method
from math import sqrt
  
MAX = 100000
  
# Create a boolean array "prime[0..n]" 
# and initialize make all entries of 
# boolean array 'prime' as true. 
# A value in prime[i] will finally be 
# false if i is Not a prime, else true. 
prime = [True] * MAX
  
arr = []
  
# Utility function to generate
# prime numbers 
def SieveOfEratosthenes() :
  
    for p in range(2, int(sqrt(MAX)) + 1) :
  
        # If prime[p] is not changed, 
        # then it is a prime 
        if prime[p] == True :
  
            # Update all multiples of p 
            for i in range(p * 2 , MAX, p) :
                prime[i] = False
  
    # store all prime numbers 
    # to list 'arr' 
    for p in range(2, MAX) :
  
        if prime[p] :
            arr.append(p)
      
# Function to check the number 
# for Primorial prime 
def isPrimorialPrime(n) :
  
    # If n is not prime Number 
    # return flase 
    if not prime[n] :
        return False
  
    product, i = 1, 0
  
    # Multiply next prime number 
    # and check if product + 1 = n 
    # or Product-1 = n holds or not 
    while product < n :
  
        product *= arr[i]
  
        if product + 1 == n or product - 1 == n :
            return True
  
        i += 1
  
    return False
  
# Driver code
if __name__ == "__main__" :
      
    SieveOfEratosthenes()
      
    n = 31
  
    # Check if n is Primorial Prime 
    if (isPrimorialPrime(n)) :
        print("YES") 
    else :
        print("NO") 
      
# This code is contributed by ANKITRAI1


C#
// c# program to check Primorial prime 
using System;
using System.Collections.Generic;
  
public class GFG
{
  
    public const int MAX = 1000000;
    public static List arr = new List();
    public static bool[] prime = new bool[MAX];
  
    // Function to get the prime numbers 
    public static void SieveOfEratosthenes()
    {
  
        // make all entries of boolean array 'prime' 
        // as true. A value in prime[i] will 
        // finally be false if i is Not a prime, else true. 
  
        for (int i = 0; i < MAX; i++)
        {
            prime[i] = true;
        }
  
        for (int p = 2; p * p < MAX; p++)
        {
  
            // If prime[p] is not changed, then it is a prime 
            if (prime[p] == true)
            {
  
                // Update all multiples of p 
                for (int i = p * 2; i < MAX; i += p)
                {
                    prime[i] = false;
                }
            }
        }
  
        // store all prime numbers 
        // to vector 'arr' 
        for (int p = 2; p < MAX; p++)
        {
            if (prime[p])
            {
                arr.Add(p);
            }
        }
    }
  
    // Function to check the number for Primorial prime 
    public static bool isPrimorialPrime(int n)
    {
        // If n is not prime 
        // Then return false 
        if (!prime[n])
        {
            return false;
        }
  
        long product = 1;
        int i = 0;
        while (product < n)
        {
  
            // Multiply next prime number 
            // and check if product + 1 = n or product -1=n 
            // holds or not 
            product = product * arr[i];
  
            if (product + 1 == n || product - 1 == n)
            {
                return true;
            }
  
            i++;
        }
  
        return false;
    }
  
    // Driver Code 
    public static void Main(string[] args)
    {
        SieveOfEratosthenes();
  
        int n = 31;
  
        if (isPrimorialPrime(n))
        {
            Console.WriteLine("YES");
        }
        else
        {
            Console.WriteLine("NO");
        }
    }
}
  
// This code is contributed by Shrikant13


PHP


输出:
YES