- Related Questions & Answers
- Count ways to express a number as sum of consecutive numbers in C++
- Count ways to express ‘n’ as sum of odd integers in C++
- Count ways to express a number as sum of powers in C++
- Minimum number of palindromes required to express N as a sum using C++.
- Check if a prime number can be expressed as sum of two Prime Numbers in Python
- C program for a number to be expressed as a sum of two prime numbers.
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime in C++
- Print prime numbers with prime sum of digits in an array
- Minimum numbers needed to express every integer below N as a sum in C++
- C++ Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers
- Sum of all prime numbers in an array - JavaScript
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array?
- Sum of all prime numbers in JavaScript
- Prime numbers after prime P with sum S in C++
- Squared sum of n odd numbers - JavaScript

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

In this problem, we are given an odd number N. Our task is to *express an odd number as the sum of prime numbers.*

There can be at max three prime numbers while expressing the number.

**Input: **N = 55

**Output: **53 + 2

**Solution Approach: **

The odd number can be represented as a sum of primes. Taking these primes into consideration we have three cases.

**Case 1: **If n is a prime number, it is represented as the sum of one prime number **n**.**Case 2: **If (n - 2) is a prime number, it is represented as the sum of two prime numbers **n-2 and 2**.

**Case 3: **( n - 3 ) is an even number which can be represented as a sum of two prime numbers using Goldbach’s conjecture method in which we will check if a number A is prime and number {(n-3) - A } is also prime then print it.

#include <iostream> using namespace std; bool isPrime(int x) { if (x == 0 || x == 1) return false; for (int i = 2; i * i <= x; ++i) if (x % i == 0) return false; return true; } void primeAsSumofPrime(int n) { if (isPrime(n) ) cout<<n; else if (isPrime(n - 2)) cout<<"2 "<<"+ "<<(n - 2); else{ cout<<"3 "<<"+ "; n -= 3; for (int i = 0; i < n; i++) { if (isPrime(i) && isPrime(n - i)) { cout<<i<<" + "<<(n - i); break; } } } } int main() { int n = 561; cout<<"The number "<<n<<" expressed as sum of primes is "; primeAsSumofPrime(n); return 0; }

The number 561 expressed as sum of primes is 3 + 11 + 547

Advertisements