📜  陈素数

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

给定一个正整数n,任务是检查它是否为Chen质数。如果给定的数字是Chen Prime数,则打印“是”,否则打印“否”。

Chen质数:在数学中,如果‘p + 2’是质数或半质数,则数’p’被称为chen质数。
半质数是两个质数的乘积。

Chen的前几个素数是:

例子:

Input : 11
Output: YES
Explanation: 11 is prime number and 11+2 
(i.e 13 is also prime number)

Input : 7
Output: YES
Explanation: 7 is prime number and 7+2 
( i.e 9 ) is a semi prime number 

先决条件

  • 素数
  • 半素数

方法:

  1. 检查给定的数字-‘n’是否为质数。
  2. 如果n是质数:
    • 检查n + 2是素数还是半素数
    • 如果n + 2是质数或半质数,则打印’YES’
    • 否则打印“否”
  3. 如果n不是质数,则打印’NO’。

下面是上述想法的实现

CPP
// CPP program to check
// Chen prime number
  
#include 
using namespace std;
  
// Utility function to check whether
// number is semiprime or not
int isSemiprime(int num)
{
    int cnt = 0;
  
    for (int i = 2; cnt < 2 && i * i <= num; ++i)
        while (num % i == 0)
            num /= i, ++cnt; // Increment count
    // of prime numbers
  
    // If number is greater than 1, add it to
    // the count variable as it indicates the
    // number remain is prime number
    if (num > 1)
        ++cnt;
  
    // Return '1' if count is equal to '2' else
    // return '0'
    return cnt == 2;
}
  
// Utility function to check whether
// the given 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 to check Chen prime number
bool isChenPrime(int n)
{
  
    if (isPrime(n) && (isSemiprime(n + 2) || isPrime(n + 2)))
        return true;
    else
        return false;
}
  
// Driver code
int main()
{
    int n = 7;
  
    if (isChenPrime(n))
        cout << "YES";
    else
        cout << "NO";
  
    return 0;
}


Java
// JAVA program to check
// Chen Prime number
  
class GFG {
    // Utility function to check
    // if the given number is semi-prime or not
    static boolean isSemiPrime(int num)
    {
        int cnt = 0;
  
        for (int i = 2; cnt < 2 && i * i <= num; ++i)
  
            while (num % i == 0) {
                num /= i;
  
                // Increment count
                // of prime numbers
                ++cnt;
            }
  
        // If number is greater than 1,
        // add it to the count variable
        // as it indicates the number
        // remain is prime number
        if (num > 1)
            ++cnt;
  
        // Return '1' if count is equal
        // to '2' else return '0'
        return cnt == 2 ? true : false;
    }
  
    // 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 chen prime number
    static boolean isChenPrime(int n)
    {
  
        if (isPrime(n) && (isSemiPrime(n + 2) || isPrime(n + 2)))
            return true;
        else
            return false;
    }
    // Driver code
    public static void main(String[] args)
    {
  
        int n = 7;
  
        if (isChenPrime(n))
            System.out.println("YES");
        else
            System.out.println("NO");
    }
}


C#
// C# program to check
// Chen Prime number
using System;
class GFG {
    // Utility function to check
    // if the given number is semi-prime or not
    static bool isSemiPrime(int num)
    {
        int cnt = 0;
  
        for (int i = 2; cnt < 2 && i * i <= num; ++i)
  
            while (num % i == 0) {
                num /= i;
  
                // Increment count
                // of prime numbers
                ++cnt;
            }
  
        // If number is greater than 1,
        // add it to the count variable
        // as it indicates the number
        // remain is prime number
        if (num > 1)
            ++cnt;
  
        // Return '1' if count is equal
        // to '2' else return '0'
        return cnt == 2 ? true : false;
    }
  
    // 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 chen prime number
    static bool isChenPrime(int n)
    {
  
        if (isPrime(n) && (isSemiPrime(n + 2) || isPrime(n + 2)))
            return true;
        else
            return false;
    }
    // Driver code
    public static void Main()
    {
  
        int n = 7;
  
        if (isChenPrime(n))
            Console.WriteLine("YES");
        else
            Console.WriteLine("NO");
    }
}


Python3
# Python3 program to check
# Chen Prime number
  
import math
          
# Utility function to Check 
# Semi-prime number 
  
def isSemiPrime(num): 
    cnt = 0
    
    for i in range(2, int(math.sqrt(num)) + 1): 
        while num % i == 0: 
            num /= i 
            cnt += 1 # Increment count 
                    # of prime number 
    
        # If count is greater than 2, 
        # break loop  
        if cnt >= 2:  
            break
    # If number is greater than 1, add it to 
    # the count variable as it indicates the 
    # number remain is prime number 
    if(num > 1): 
        cnt += 1
    
    # Return '1' if count is equal to '2' else 
    # return '0' 
    return cnt == 2
   
   
  
# 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 Chen prime number or not
          
def isChenPrime( n):
  
    if(isPrime(n) and (isSemiPrime(n + 2) or isPrime(n + 2))):
        return True
    else:
        return False
              
# Driver code
  
n = 7
  
if(isChenPrime(n)):
    print("YES");
else:
    print("NO");


输出:
YES