📌  相关文章
📜  将一个数字表示为最大可能质数的总和

📅  最后修改于: 2021-04-24 16:38:40             🧑  作者: Mango

给定一个正整数N 。任务是将其表示为最大可能质数的总和。 (N> 1)

例子

Input : N = 5 
Output : 2 3 

Input : N = 6
Output : 2 2 2

起初,这个问题似乎涉及戈德巴赫猜想的某种运用。但是这里的主要观察是最大程度地使用术语,您应该使用尽可能少的术语。这导致以下想法:

  • 如果N是偶数,则可以表示为 n/2 两个。
  • 除此以外,  n-3 必须是偶数,因此N可以表示为1与3的和 (n-3)/2 两个。

这是总和为N的素数的最大数目。

下面是上述方法的实现:

C++
// CPP program to represent a number as a
// sum of maximum possible number of
// Prime Numbers
#include 
using namespace std;
  
// Function to represent a number as a
// sum of the maximum possible number
// of Prime Numbers
void printAsMaximalPrimeSum(int n)
{
    // If n is odd, print one 3
    if (n % 2 == 1) {
        cout << "3 ";
        n -= 3;
    }
  
    // Now n is even, print 2 n/2 times
    while (n) {
        cout << "2 ";
        n -= 2;
    }
}
  
// Driver Code
int main()
{
    int n = 5;
    printAsMaximalPrimeSum(n);
}


Java
// Java program to represent a number as a
// sum of maximum possible number of
// Prime Numbers
  
import java.io.*;
  
class GFG {
     
// Function to represent a number as a
// sum of the maximum possible number
// of Prime Numbers
static void printAsMaximalPrimeSum(int n)
{
    // If n is odd, print one 3
    if (n % 2 == 1) {
        System.out.print( "3 ");
        n -= 3;
    }
  
    // Now n is even, print 2 n/2 times
    while (n>0) {
        System.out.print( "2 ");
        n -= 2;
    }
}
  
       // Driver Code
    public static void main (String[] args) {
    int n = 5;
    printAsMaximalPrimeSum(n);
    }
}
  
// This Code is contributed by inder_verma..


Python3
# Python3 program to represent a number as a
# sum of maximum possible number of
# Prime Numbers
  
  
# Function to represent a number as a
# sum of the maximum possible number
# of Prime Numbers
def printAsMaximalPrimeSum( n):
   
    # If n is odd, print one 3
    if ( n % 2 == 1):  
        print("3 ",end="") 
        n -= 3 
       
  
    # Now n is even, print 2 n/2 times
    while ( n>0):  
        print("2 ",end="")
        n -= 2 
       
   
      
# Driver Code
  
n = 5 
printAsMaximalPrimeSum( n) 
  
# This code is contributed by ihritik


C#
// C# program to represent a number as a
// sum of maximum possible number of
// Prime Numbers
  
  
using System;
class GFG
{
    // Function to represent a number as a
    // sum of the maximum possible number
    // of Prime Numbers
    static void printAsMaximalPrimeSum(int n)
    {
        // If n is odd, print one 3
        if (n % 2 == 1) {
            Console.Write("3 ");
            n -= 3;
        }
      
        // Now n is even, print 2 n/2 times
        while (n>0) {
            Console.Write("2 ");
            n -= 2;
        }
    }
      
    // Driver Code
    public static void Main()
    {
        int n = 5;
        printAsMaximalPrimeSum(n);
    }
  
}
  
// This code is contributed by ihritik


PHP
0) {
        echo "2 ";
        $n -= 2;
    }
}
      
// Driver Code
  
$n = 5;
printAsMaximalPrimeSum($n);
  
  
// This code is contributed by ihritik
?>


输出:
3 2

时间复杂度: O(N)