📜  求素的总和等于N的最小素数

📅  最后修改于: 2021-05-07 00:27:49             🧑  作者: Mango

给定一个大于1的正整数N ,则任务是求和等于给定N的素数的最小计数。

例子:

方法:
对于总和为给定数N的最小素数,素数必须尽可能大。以下是上述问题陈述的观察结果:

  • 情况1:如果数字为质数,则求和N所需的最小质数为1
  • 情况2:如果数字为偶数,则对于每个大于2的偶数,根据哥德巴赫猜想,它可以表示为两个素数之和。因此,使和N所需的最小素数为2
  • 情况3:如果数字为奇数:
    1. 如果(N-2)为素数,则使给定总和N所需的最小素数为2
    2. 否则,使给定总和N所需的最小素数为3,因为:
      As N is odd, then (N - 3) is even.
      Hence As per case 2:
      The minimum prime number required to make the sum (N-3) is 2.
      Therefore,
      The minimum prime number required to make the sum N is 3(2+1).
      

步骤如下:

  1. 通过使用本文讨论的方法,检查给定数字N是否为质数。如果是,则打印1
  2. 除上述情况外,其他情况还会打印得出给定总和N所需的最小质数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
  
// Function to check if n is prime
bool isPrime(int n)
{
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}
  
// Function to count the minimum
// prime required for given sum N
void printMinCountPrime(int N)
{
  
    int minCount;
  
    // Case 1:
    if (isPrime(N)) {
        minCount = 1;
    }
  
    // Case 2:
    else if (N % 2 == 0) {
        minCount = 2;
    }
  
    // Case 3:
    else {
  
        // Case 3a:
        if (isPrime(N - 2)) {
            minCount = 2;
        }
  
        // Case 3b:
        else {
            minCount = 3;
        }
    }
  
    cout << minCount << endl;
}
  
// Driver Code
int main()
{
    int N = 100;
  
    // Function Call
    printMinCountPrime(N);
    return 0;
}


Java
// Java program for the above approach
class GFG{
  
// Function to check if n is prime
static boolean isPrime(int n)
{
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}
  
// Function to count the minimum
// prime required for given sum N
static void printMinCountPrime(int N)
{
  
    int minCount;
  
    // Case 1:
    if (isPrime(N)) {
        minCount = 1;
    }
  
    // Case 2:
    else if (N % 2 == 0) {
        minCount = 2;
    }
  
    // Case 3:
    else {
  
        // Case 3a:
        if (isPrime(N - 2)) {
            minCount = 2;
        }
  
        // Case 3b:
        else {
            minCount = 3;
        }
    }
  
    System.out.print(minCount +"\n");
}
  
// Driver Code
public static void main(String[] args)
{
    int N = 100;
  
    // Function Call
    printMinCountPrime(N);
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach 
  
# Function to check if n is prime 
def isPrime(n) : 
  
    for i in range(2, int(n ** (1/2)) + 1) :
        if (n % i == 0) :
            return False; 
      
    return True; 
  
# Function to count the minimum 
# prime required for given sum N 
def printMinCountPrime(N) : 
  
    # Case 1: 
    if (isPrime(N)) :
        minCount = 1; 
  
    # Case 2: 
    elif (N % 2 == 0) :
        minCount = 2; 
  
    # Case 3: 
    else : 
  
        # Case 3a: 
        if (isPrime(N - 2)) :
            minCount = 2; 
  
        # Case 3b: 
        else :
            minCount = 3; 
  
    print(minCount) ; 
  
# Driver Code 
if __name__ == "__main__" : 
    N = 100; 
  
    # Function Call 
    printMinCountPrime(N); 
  
# This code is contributed by AnkitRai01


C#
// C# program for the above approach
using System;
  
class GFG{
  
// Function to check if n is prime
static bool isPrime(int n)
{
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}
  
// Function to count the minimum
// prime required for given sum N
static void printMinCountPrime(int N)
{
  
    int minCount;
  
    // Case 1:
    if (isPrime(N)) {
        minCount = 1;
    }
  
    // Case 2:
    else if (N % 2 == 0) {
        minCount = 2;
    }
  
    // Case 3:
    else {
  
        // Case 3a:
        if (isPrime(N - 2)) {
            minCount = 2;
        }
  
        // Case 3b:
        else {
            minCount = 3;
        }
    }
  
    Console.WriteLine(minCount +"\n");
}
  
// Driver Code
public static void Main(string[] args)
{
    int N = 100;
  
    // Function Call
    printMinCountPrime(N);
}
}
  
// This code is contributed by AnkitRai01


输出:
2

时间复杂度: O(&Sqrt; N),其中N是给定的数字。