# Express an odd number as sum of prime numbers

Given an odd number, we need to express it as the sum of at most three prime numbers.

Examples :

```Input : 27
Output : 27 = 3 + 5 + 19

Input : 15
Output : 15 = 2 + 13
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach : Here, we use Goldbach’s conjecture to solve this problem. It says that any even integer can be expressed as sum of two prime numbers.
We have three cases here:
1) When N is a prime number, print the number.
2) When (N-2) is a prime number, print 2 and N-2.
3) Express N as 3 + (N-3). Obviously, N-3 will be an even number (subtraction of an odd from another odd results in even). So, according to Goldbach’s conjecture, it can be expressed as the sum of two prime numbers. So, print 3 and other two prime numbers.

## C++

 `// CPP program to express N as sum of at-most ` `// three prime numbers. ` `#include ` `using` `namespace` `std; ` ` `  `// Function to check if a number is prime or not. ` `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``; ` `} ` ` `  `// Prints at most three prime numbers whose ` `// sum is n. ` `void` `findPrimes(``int` `n) ` `{ ` `    ``if` `(isPrime(n)) ``// CASE-I     ` `        ``cout << n << endl; ` `     `  `    ``else` `if` `(isPrime(n - 2)) ``// CASE-II     ` `        ``cout << 2 << ``" "` `<< n - 2 << endl; ` ` `  `    ``else` `// CASE-III ` `    ``{ ` `        ``cout << 3 << ``" "``; ` `        ``n = n - 3; ` `        ``for` `(``int` `i = 0; i < n; i++) { ` `            ``if` `(isPrime(i) && isPrime(n - i)) { ` `                ``cout << i << ``" "` `<< (n - i); ` `                ``break``; ` `            ``} ` `        ``} ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 27; ` `    ``findPrimes(n);  ` `    ``return` `0; ` `} `

## Java

 `// Java program to express N as sum  ` `// of at-most three prime numbers. ` `import` `java.util.*; ` ` `  `class` `GFG{ ` `     `  `    ``// Function to check if a  ` `    ``// number is prime or not. ` `    ``public` `static` `boolean` `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``; ` `    ``} ` ` `  `     `  `    ``// Prints at most three prime  ` `    ``// numbers whose sum is n. ` `    ``public` `static` `void` `findPrimes(``int` `n) ` `    ``{ ` `        ``if` `(isPrime(n)) ``// CASE-I  ` `            ``System.out.print( n ); ` `     `  `        ``else` `if` `(isPrime(n - ``2``)) ``// CASE-II  ` `            ``System.out.print( ``2` `+ ``" "` `+  ` `                              ``(n - ``2``) ); ` ` `  `        ``else` `// CASE-III ` `        ``{ ` `            ``System.out.print( ``3` `+ ``" "``); ` `            ``n = n - ``3``; ` `             `  `            ``for` `(``int` `i = ``0``; i < n; i++) { ` `                ``if` `(isPrime(i) && isPrime(n - i)) { ` `                    ``System.out.print( i + ``" "` `+  ` `                                         ``(n - i)); ` `                    ``break``; ` `                ``} ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `n = ``27``; ` `        ``findPrimes(n); ` `    ``} ` `} ` ` `  `// This code is contributed by rishabh_jain `

## Python3

 `# Python3 program to express N as  ` `# sum of at-most three prime numbers ` ` `  `# Function to check if a number ` `# is prime or not. ` `def` `isPrime(x): ` `    ``if``(x ``=``=` `0` `or` `x ``=``=` `1``) : ` `        ``return` `0` `    ``i ``=` `2` `    ``while` `i ``*` `i <``=` `x :  ` `        ``if` `(x ``%` `i ``=``=` `0``) : ` `            ``return` `0` `        ``i ``=` `i ``+` `1` `    ``return` `1` ` `  `# Prints at most three prime numbers  ` `# whose sum is n. ` `def` `findPrimes(n) : ` `    ``if` `(isPrime(n)): ` `         `  `        ``# CASE-I  ` `        ``print``(n, end ``=` `" "``) ` `     `  `    ``elif` `(isPrime(n ``-` `2``)) : ` `         `  `        ``# CASE-II  ` `        ``print` `(``"2"``, end ``=` `" "``) ` `        ``print` `(n ``-` `2``, end ``=` `" "` `) ` ` `  `    ``else``: ` `        ``#CASE-III ` `        ``print` `( ``"3"``, end ``=` `" "` `) ` `        ``n ``=` `n ``-` `3` `        ``i ``=` `0` `        ``while` `i < n : ` `            ``if` `(isPrime(i) ``and` `isPrime(n ``-` `i)) : ` `                ``print``(i, end ``=` `" "``) ` `                ``print` `((n ``-` `i), end ``=` `" "``) ` `                ``break` `            ``i ``=` `i ``+` `1` ` `  `# Driver Code ` `n ``=` `27``; ` `findPrimes(n);  ` ` `  `# This code is contributed by rishabh_jain `

## C#

 `// C# program to express N as sum  ` `// of at-most three prime numbers. ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `    ``// Function to check if a  ` `    ``// number is prime or not. ` `    ``public` `static` `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``; ` `    ``} ` ` `  `     `  `    ``// Prints at most three prime  ` `    ``// numbers whose sum is n. ` `    ``public` `static` `void` `findPrimes(``int` `n) ` `    ``{ ` `        ``if` `(isPrime(n)) ``// CASE-I  ` `            ``Console.WriteLine( n ); ` `     `  `        ``else` `if` `(isPrime(n - 2)) ``// CASE-II  ` `            ``Console.Write( 2 + ``" "` `+  ` `                            ``(n - 2) ); ` ` `  `        ``else` `// CASE-III ` `        ``{ ` `            ``Console.Write( 3 + ``" "``); ` `            ``n = n - 3; ` `             `  `            ``for` `(``int` `i = 0; i < n; i++) { ` `                ``if` `(isPrime(i) && isPrime(n - i))  ` `                ``{ ` `                    ``Console.WriteLine( i + ``" "` `+  ` `                                        ``(n - i)); ` `                    ``break``; ` `                ``} ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `n = 27; ` `        ``findPrimes(n); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m `

## PHP

 ` `

Output :

```3 5 19
```

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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : jit_t