# Maximum Primes whose sum is equal to given N

Given a positive integer **N > 1**. Find the maximum count of prime numbers whose sum is equal to given N.

**Examples:**

Input :N = 5

Output :2

Explanation: 2 and 3 are two prime numbers whose sum is 5.

Input :N = 6

Output :3

Explanation: 2, 2, 2 are three prime numbers whose sum is 6.

For maximum number of primes whose sum is equal to given n, prime numbers must be as small as possible. So, 2 is smallest possible prime number and is an even number. Next prime number greater than 2 is 3 which is odd. So, for any given n there are two conditions, either n will be odd or even.

**Case 1 : n is even**, In this case**n/2**will be the answer (n/2 number of 2 will result into sum of n).-
**Case 1 : n is odd**, In this case**floor(n/2)**will be the answer ((n-3)/2 number of 2 and one 3 will result into sum of n.

Below is the implementation of the above approach:

## C++

`// C++ program for above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find max count of primes ` `int` `maxPrimes(` `int` `n) ` `{ ` ` ` `// if n is even n/2 is required answer ` ` ` `// if n is odd floor(n/2) = (int)(n/2) is required answer ` ` ` `return` `n / 2; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 17; ` ` ` ` ` `cout << maxPrimes(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for above approach ` `class` `GFG ` `{ ` ` ` `// Function to find max count of primes ` `static` `int` `maxPrimes(` `int` `n) ` `{ ` ` ` `// if n is even n/2 is required answer ` ` ` `// if n is odd floor(n/2) = (int)(n/2) ` ` ` `// is required answer ` ` ` `return` `n / ` `2` `; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `17` `; ` ` ` ` ` `System.out.println(maxPrimes(n)); ` `} ` `} ` ` ` `// This code is contributed ` `// by Code_Mech ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for above approach ` ` ` `# Function to find max count of primes ` `def` `maxPrmimes(n): ` ` ` ` ` `# if n is even n/2 is required answer ` ` ` `# if n is odd floor(n/2) = (int)(n/2) ` ` ` `# is required answer ` ` ` `return` `n ` `/` `/` `2` ` ` `# Driver code ` `n ` `=` `17` `print` `(maxPrmimes(n)) ` ` ` `# This code is contributed ` `# by Shrikant13 ` |

*chevron_right*

*filter_none*

## C#

`// C# program for above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find max count of primes ` `static` `int` `maxPrimes(` `int` `n) ` `{ ` ` ` `// if n is even n/2 is required answer ` ` ` `// if n is odd floor(n/2) = (int)(n/2) ` ` ` `// is required answer ` ` ` `return` `n / 2; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `n = 17; ` ` ` ` ` `Console.WriteLine(maxPrimes(n)); ` `} ` `} ` ` ` `// This code is contributed ` `// by Akanksha Rai ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program for above approach ` ` ` `// Function to find max count of primes ` `function` `maxPrimes(` `$n` `) ` `{ ` ` ` `// if n is even n/2 is required answer ` ` ` `// if n is odd floor(n/2) = (int)(n/2) is required answer ` ` ` `return` `(int)(` `$n` `/ 2); ` `} ` ` ` ` ` `// Driver Code ` ` ` `$n` `= 17; ` ` ` `echo` `maxPrimes(` `$n` `); ` ` ` `// This code is contributed by mits ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

8

## Recommended Posts:

- Minimum number of single digit primes required whose sum is equal to N
- Length of largest sub-array having primes strictly greater than non-primes
- Count primes that can be expressed as sum of two consecutive primes and 1
- Find three integers less than or equal to N such that their LCM is maximum
- Find four factors of N with maximum product and sum equal to N
- Find four factors of N with maximum product and sum equal to N | Set-2
- Find four factors of N with maximum product and sum equal to N | Set 3
- Find maximum product of digits among numbers less than or equal to N
- Maximum count of equal numbers in an array after performing given operations
- Circular primes less than n
- Palindromic Primes
- Product of Primes of all Subsets
- Count Primes in Ranges
- Alternate Primes till N
- Product of all primes in the range from L to R

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.