- Related Questions & Answers
- Print triplets with sum less than or equal to k in C Program
- How to check if two dates are equal in Java 8?
- The Maximum Data Rate of a Channel
- How do you find continuous sub array whose sum is equal to a given number in Java?
- How to find all pairs of elements in Java array whose sum is equal to a given number?
- All possible binary numbers of length n with equal sum in both halves?
- Alternate Primes till N in C++?
- C/C++ Program for Maximum height when coins are arranged in a triangle?
- Get the maximum count of distinct values in a separate column with MySQL
- How to get the data associated with the maximum id in a MySQL table?
- Getting the maximum value from a varchar field in MySQL
- Finding the minimum and maximum value from a string with numbers separated by hyphen in MySQL?
- Limiting numbers to a maximum value in MySQL?
- How to find maximum value in an array using spread operator in JavaScript?
- C++ Program to Find the maximum subarray sum using Binary Search approach

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

If N = 8 then answer will be 4 as − 2 + 2 + 2 + 2 = 8

- For maximum number of primes whose sum is equal to given n, prime numbers must be as small as possible
- 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 −
- If n is an even, then n/2 will be the answer
- If n is an odd, then floor(n/2) will be the answer

#include <bits/stdc++.h> using namespace std; int getMaxPrimes(int n) { if (n % 2 == 0) { return n / 2; } return floor(n/2); } int main() { int n = 8; cout << "Maximum prime numbers to get sum " << n << " are = " << getMaxPrimes(n) << endl; return 0; }

When you compile and execute above program. It generates following output −

Maximum prime numbers to get sum 8 are = 4

Advertisements