# Check if a prime number can be expressed as sum of two Prime Numbers

Given a prime number . The task is to check if it is possible to express as sum of two separate prime numbers.

Note: The range of N is less than 108.

Examples:

```Input : N = 13
Output : Yes
Explanation : The number 13 can be written as 11 + 2,
here 11 and 2 are both prime.

Input : N = 11
Output : No
```

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

Simple Solution: A simple solution is to create a sieve to store all the prime numbers less than the number N. Then run a loop from 1 to N and check whether and are both prime or not. If yes then print Yes, else No.

Efficient solution: Apart from 2, all of the prime numbers are odd. So it is not possible to represent a prime number (which is odd) to be written as a sum of two odd prime numbers, so we are sure that one of the two prime number should be 2. So we have to check whether n-2 is prime or not. If it holds we print Yes else No.

For example, if the number is 19 then we have to check whether 19-2 = 17 is a prime number or not. If 17 is a prime number then print yes otherwise print no.

Below is the implementation of the above approach:

## C++

 `// C++ program to check if a prime number ` `// can be expressed as sum of ` `// two Prime Numbers ` `#include ` `using` `namespace` `std; ` ` `  `// Function to check whether a number ` `// is prime or not ` `bool` `isPrime(``int` `n) ` `{ ` `    ``if` `(n <= 1) ` `        ``return` `false``; ` ` `  `    ``for` `(``int` `i = 2; i <= ``sqrt``(n); i++) { ` `        ``if` `(n % i == 0) ` `            ``return` `false``; ` `    ``} ` ` `  `    ``return` `true``; ` `} ` ` `  `// Function to check if a prime number ` `// can be expressed as sum of ` `// two Prime Numbers ` `bool` `isPossible(``int` `N) ` `{ ` `    ``// if the number is prime, ` `    ``// and number-2 is also prime ` `    ``if` `(isPrime(N) && isPrime(N - 2)) ` `        ``return` `true``; ` `    ``else` `        ``return` `false``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 13; ` ` `  `    ``if` `(isPossible(n)) ` `        ``cout << ``"Yes"``; ` `    ``else` `        ``cout << ``"No"``; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to check if a prime number ` `// can be expressed as sum of ` `// two Prime Numbers ` ` `  `public` `class` `GFG{ ` `     `  `    ``// Function to check whether a number ` `    ``// is prime or not ` `    ``static` `boolean` `isPrime(``int` `n) ` `    ``{ ` `        ``if` `(n <= ``1``) ` `            ``return` `false``; ` `     `  `        ``for` `(``int` `i = ``2``; i <= Math.sqrt(n); i++) { ` `            ``if` `(n % i == ``0``) ` `                ``return` `false``; ` `        ``} ` `     `  `        ``return` `true``; ` `    ``} ` `     `  `    ``// Function to check if a prime number ` `    ``// can be expressed as sum of ` `    ``// two Prime Numbers ` `    ``static` `boolean` `isPossible(``int` `N) ` `    ``{ ` `        ``// if the number is prime, ` `        ``// and number-2 is also prime ` `        ``if` `(isPrime(N) && isPrime(N - ``2``)) ` `            ``return` `true``; ` `        ``else` `            ``return` `false``; ` `    ``} ` `     `  `     ``// Driver code ` `     ``public` `static` `void` `main(String []args){ ` `          `  `        ``int` `n = ``13``; ` `     `  `        ``if` `(isPossible(n) == ``true``) ` `            ``System.out.println(``"Yes"``); ` `        ``else` `            ``System.out.println(``"No"``); ` `     ``} ` `     ``// This code is contributed by ANKITRAI1 ` `} `

## Python3

 `# Python 3 program to check if a prime  ` `# number can be expressed as sum of  ` `# two Prime Numbers  ` `import` `math ` ` `  `# Function to check whether a number  ` `# is prime or not  ` `def` `isPrime(n): ` `    ``if` `n <``=` `1``: ` `        ``return` `False` `     `  `    ``if` `n ``=``=` `2``: ` `        ``return` `True` `         `  `    ``if` `n``%``2` `=``=` `0``: ` `        ``return` `False` `         `  `    ``for` `i ``in` `range``(``3``, ``int``(math.sqrt(n))``+``1``, ``2``): ` `        ``if` `n``%``i ``=``=` `0``: ` `            ``return` `False` `    ``return` `True` ` `  `# Function to check if a prime number  ` `# can be expressed as sum of  ` `# two Prime Numbers  ` `def` `isPossible(n): ` ` `  `    ``# if the number is prime,  ` `    ``# and number-2 is also prime  ` `    ``if` `isPrime(n) ``and` `isPrime(n ``-` `2``): ` `        ``return` `True` `    ``else``: ` `        ``return` `False` ` `  `# Driver code ` `n ``=` `13` `if` `isPossible(n) ``=``=` `True``: ` `    ``print``(``"Yes"``) ` `else``: ` `    ``print``(``"No"``) ` `     `  `# This code is contributed by Shrikant13 `

## C#

 `// C# program to check if a prime  ` `// number can be expressed as sum  ` `// of two Prime Numbers ` `using` `System; ` ` `  `class` `GFG ` `{ ` ` `  `// Function to check whether a  ` `// number is prime or not ` `static` `bool` `isPrime(``int` `n) ` `{ ` `    ``if` `(n <= 1) ` `        ``return` `false``; ` ` `  `    ``for` `(``int` `i = 2;  ` `             ``i <= Math.Sqrt(n); i++)  ` `    ``{ ` `        ``if` `(n % i == 0) ` `            ``return` `false``; ` `    ``} ` ` `  `    ``return` `true``; ` `} ` ` `  `// Function to check if a prime  ` `// number can be expressed as sum  ` `// of two Prime Numbers ` `static` `bool` `isPossible(``int` `N) ` `{ ` `    ``// if the number is prime, ` `    ``// and number-2 is also prime ` `    ``if` `(isPrime(N) && isPrime(N - 2)) ` `        ``return` `true``; ` `    ``else` `        ``return` `false``; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main() ` `{ ` `    ``int` `n = 13; ` ` `  `    ``if` `(isPossible(n) == ``true``) ` `        ``Console.Write(``"Yes"``); ` `    ``else` `        ``Console.Write(``"No"``); ` `} ` `} ` ` `  `// This code is contributed  ` `// by ChitraNayal `

## PHP

 ` `

Output:

```Yes
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

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.

Article Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.