# Represent a number as a Sum of Maximum Possible Number of Prime Numbers in C++

C++Server Side ProgrammingProgramming

<p>Discuss a problem where we are given a number N, and we need to split this number in the maximum prime numbers sum, for example</p><pre class="result notranslate">Input: N = 7 Output: 2 2 3 Explanation: 7 can be represented as the sum of two 2&rsquo;s and a 3 which are the maximum possible prime numbers. Input : N = 17 Output: 2 2 2 2 2 2 2 3</pre><h2>Approach to Find the Solution</h2><p>To represent a number in prime numbers, we can subtract a prime number with N and check the difference for prime. If the difference is prime, then we can represent N as the addition of two prime numbers.</p><p>But here, we have to find a maximum number of prime numbers, and for that, we should take minimum prime numbers, i.e., 2 and 3. We can form any number with a sum of 2&rsquo;s and 3&rsquo;s.</p><ul class="list"><li><p>Check the number of even; If it is even, it can be formed by the sum of ( N/2 ) 2&rsquo;s.</p></li><li><p>It can be formed by one three and [ (N-3) / 2 ] 2&rsquo;s if it is odd.</p></li><li><p>In this way, we can represent N by the sum of a maximum number of primes.</p></li></ul><h2>Example</h2><pre class="demo-code notranslate language-cpp" data-lang="cpp"> #include &lt;bits/stdc++.h&gt; using namespace std; int main(){ &nbsp; &nbsp;int N = 7; &nbsp; &nbsp;// checking if N is odd, &nbsp; &nbsp;// If yes, then print 3 &nbsp; &nbsp;// and subtract 3 from N. &nbsp; &nbsp;if (N &amp; 1 == 1) { &nbsp; &nbsp; &nbsp; cout &lt;&lt; &quot;3 +&quot;; &nbsp; &nbsp; &nbsp; N -= 3; &nbsp; &nbsp;} &nbsp; &nbsp;// // keep subtracting and printing 2 &nbsp; &nbsp;// until N is becomes 0. &nbsp; &nbsp;while (N!=2) { &nbsp; &nbsp; &nbsp; cout &lt;&lt; &quot; 2 +&quot;; &nbsp; &nbsp; &nbsp; N -= 2; &nbsp; &nbsp;} &nbsp; &nbsp;cout &lt;&lt; &quot; 2&quot;; &nbsp; &nbsp;return 0; } </pre><h2>Output</h2><pre class="result notranslate">3 + 2 + 2</pre><h2>Conclusion</h2><p>In this tutorial, we discussed representing a number as a sum of the maximum number of prime numbers. We discussed a simple approach to solve this problem by representing numbers as a sum of 2&rsquo;s and 3&rsquo;s. We also discussed the C++ program for this problem which we can do with programming languages like C, Java, Python, etc. We hope you find this tutorial helpful.</p>