Given a positive integer . The task is to represent it as a sum of the maximum possible number of prime numbers. (N > 1)

**Examples**:

Input : N = 5 Output : 2 3 Input : N = 6 Output : 2 2 2

At first, the problem might seem to involve some use of Goldbach’s conjecture. But the key observation here is to maximise the number of terms used, you should use as small numbers as possible. This leads to the following idea:

- If N is even, it can be represented as sum of two’s.
- Otherwise, has to be even and hence N can be represented as sum of one 3 and two’s.

This is the maximum number of primes whose sum is N.

Below is the implementation of the above approach:

## C++

`// CPP program to represent a number as a ` `// sum of maximum possible number of ` `// Prime Numbers ` `#include <bits/stdc++.h> ` `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); ` `} ` |

*chevron_right*

*filter_none*

## 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.. ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP 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 ` `function` `printAsMaximalPrimeSum(` `$n` `) ` `{ ` ` ` `// If n is odd, print one 3 ` ` ` `if` `(` `$n` `% 2 == 1) { ` ` ` `echo` `"3 "` `; ` ` ` `$n` `-= 3; ` ` ` `} ` ` ` ` ` `// Now n is even, print 2 n/2 times ` ` ` `while` `(` `$n` `>0) { ` ` ` `echo` `"2 "` `; ` ` ` `$n` `-= 2; ` ` ` `} ` `} ` ` ` `// Driver Code ` ` ` `$n` `= 5; ` `printAsMaximalPrimeSum(` `$n` `); ` ` ` ` ` `// This code is contributed by ihritik ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

3 2

**Time Complexity: ** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count of ways to represent N as sum of a prime number and twice of a square
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Represent N as sum of K even numbers
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Maximum possible prime divisors that can exist in numbers having exactly N divisors
- Check if a prime number can be expressed as sum of two Prime Numbers
- Number of distinct ways to represent a number as sum of K unique primes
- Prime numbers after prime P with sum S
- Print prime numbers with prime sum of digits in an array
- Sum of prime numbers without odd prime digits
- Count all prime numbers in a given range whose sum of digits is also prime
- Bitwise AND of the sum of prime numbers and the sum of composite numbers in an array
- Different ways to represent N as sum of K non-zero integers
- Represent (2 / N) as the sum of three distinct positive integers of the form (1 / m)
- Print the nearest prime number formed by adding prime numbers to N
- Check if a number is Prime, Semi-Prime or Composite for very large numbers
- Maximum XOR value of maximum and second maximum element among all possible subarrays

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.