📌  相关文章
📜  检查素数是否可以表示为两个素数之和

📅  最后修改于: 2021-05-04 10:44:19             🧑  作者: Mango

给定质数N  。任务是检查是否可以表达N  两个独立质数之和。
注意:N的范围小于10 8

例子:

Input : N = 13
Output : Yes
Explanation : The number 13 can be written as 11 + 2, 
here 11 and 2 are both prime.

Input : N = 11
Output : No

一个简单的解决方案:一个简单的解决方案是创建一个筛子来存储所有小于数字N的质数。然后运行从1到N的循环,并检查是否i  n-i  是否都是素数。如果是,则打印“是”,否则打印“否”。

高效的解决方案:除2外,所有质数均为奇数。因此,不可能将质数(奇数)表示为两个奇数质数之和,因此我们确定两个质数之一应为2。因此,我们必须检查n- 2是否是质数。如果保留,则打印“是”,否则打印“否”。
例如,如果数字是19,那么我们必须检查19-2 = 17是否是质数。如果17是质数,则打印“是”,否则打印“否”。

下面是上述方法的实现:

C++
// C++ program to check if a prime number
// can be expressed as sum of
// two Prime Numbers
#include 
using namespace std;
 
// Function to check whether a number
// is prime or not
bool isPrime(int n)
{
    if (n <= 1)
        return false;
 
    for (int i = 2; i <= sqrt(n); i++) {
        if (n % i == 0)
            return false;
    }
 
    return true;
}
 
// Function to check if a prime number
// can be expressed as sum of
// two Prime Numbers
bool isPossible(int N)
{
    // if the number is prime,
    // and number-2 is also prime
    if (isPrime(N) && isPrime(N - 2))
        return true;
    else
        return false;
}
 
// Driver code
int main()
{
    int n = 13;
 
    if (isPossible(n))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


Java
// Java program to check if a prime number
// can be expressed as sum of
// two Prime Numbers
 
public class GFG{
     
    // Function to check whether a number
    // is prime or not
    static boolean isPrime(int n)
    {
        if (n <= 1)
            return false;
     
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (n % i == 0)
                return false;
        }
     
        return true;
    }
     
    // Function to check if a prime number
    // can be expressed as sum of
    // two Prime Numbers
    static boolean isPossible(int N)
    {
        // if the number is prime,
        // and number-2 is also prime
        if (isPrime(N) && isPrime(N - 2))
            return true;
        else
            return false;
    }
     
     // Driver code
     public static void main(String []args){
          
        int n = 13;
     
        if (isPossible(n) == true)
            System.out.println("Yes");
        else
            System.out.println("No");
     }
     // This code is contributed by ANKITRAI1
}


Python3
# Python3 program to check if a prime
# number can be expressed as sum of
# two Prime Numbers
import math
 
# Function to check whether a number
# is prime or not
def isPrime(n):
    if n <= 1:
        return False
     
    if n == 2:
        return True
         
    if n%2 == 0:
        return False
         
    for i in range(3, int(math.sqrt(n))+1, 2):
        if n%i == 0:
            return False
    return True
 
# Function to check if a prime number
# can be expressed as sum of
# two Prime Numbers
def isPossible(n):
 
    # if the number is prime,
    # and number-2 is also prime
    if isPrime(n) and isPrime(n - 2):
        return True
    else:
        return False
 
# Driver code
n = 13
if isPossible(n) == True:
    print("Yes")
else:
    print("No")
     
# This code is contributed by Shrikant13


C#
// C# program to check if a prime
// number can be expressed as sum
// of two Prime Numbers
using System;
 
class GFG
{
 
// Function to check whether a
// number is prime or not
static bool isPrime(int n)
{
    if (n <= 1)
        return false;
 
    for (int i = 2;
             i <= Math.Sqrt(n); i++)
    {
        if (n % i == 0)
            return false;
    }
 
    return true;
}
 
// Function to check if a prime
// number can be expressed as sum
// of two Prime Numbers
static bool isPossible(int N)
{
    // if the number is prime,
    // and number-2 is also prime
    if (isPrime(N) && isPrime(N - 2))
        return true;
    else
        return false;
}
 
// Driver code
public static void Main()
{
    int n = 13;
 
    if (isPossible(n) == true)
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed
// by ChitraNayal


PHP


Javascript


输出:
Yes