Given an integer **N**, the task is to find the count of all the primes below **N** which can be expressed as the sum of two primes.

**Examples:**

Input:N = 6

Output:1

5 is the only such prime below 6.

2 + 3 = 5.

Input:N = 11

Output:2

**Approach:** Create an array prime[] where prime[i] will store whether i is prime or not using Sieve of Eratosthenes. Now for every prime from the range [1, N – 1], check whether it can be expressed as the sum of two primes using the approach discussed here.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `const` `int` `MAX = 100005; ` `bool` `prime[MAX]; ` ` ` `// Function for Sieve of Eratosthenes ` `void` `SieveOfEratosthenes() ` `{ ` ` ` ` ` `memset` `(prime, ` `true` `, ` `sizeof` `(prime)); ` ` ` ` ` `// false here indicates ` ` ` `// that it is not prime ` ` ` `prime[0] = ` `false` `; ` ` ` `prime[1] = ` `false` `; ` ` ` ` ` `for` `(` `int` `p = 2; p * p <= MAX; p++) { ` ` ` ` ` `// If prime[p] is not changed, ` ` ` `// then it is a prime ` ` ` `if` `(prime[p]) { ` ` ` ` ` `// Update all multiples of p, ` ` ` `// set them to non-prime ` ` ` `for` `(` `int` `i = p * 2; i <= MAX; i += p) ` ` ` `prime[i] = ` `false` `; ` ` ` `} ` ` ` `} ` `} ` ` ` `// Function to return the count of primes ` `// less than or equal to n which can be ` `// expressed as the sum of two primes ` `int` `countPrimes(` `int` `n) ` `{ ` ` ` `SieveOfEratosthenes(); ` ` ` ` ` `// To store the required count ` ` ` `int` `cnt = 0; ` ` ` ` ` `for` `(` `int` `i = 2; i < n; i++) { ` ` ` ` ` `// If the integer is prime and it ` ` ` `// can be expressed as the sum of ` ` ` `// 2 and a prime number ` ` ` `if` `(prime[i] && prime[i - 2]) ` ` ` `cnt++; ` ` ` `} ` ` ` ` ` `return` `cnt; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 11; ` ` ` ` ` `cout << countPrimes(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `static` `int` `MAX = ` `100005` `; ` `static` `boolean` `[]prime = ` `new` `boolean` `[MAX]; ` ` ` `// Function for Sieve of Eratosthenes ` `static` `void` `SieveOfEratosthenes() ` `{ ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < MAX; i++) ` ` ` `prime[i] = ` `true` `; ` ` ` ` ` `// false here indicates ` ` ` `// that it is not prime ` ` ` `prime[` `0` `] = ` `false` `; ` ` ` `prime[` `1` `] = ` `false` `; ` ` ` ` ` `for` `(` `int` `p = ` `2` `; p * p < MAX; p++) ` ` ` `{ ` ` ` `// If prime[p] is not changed, ` ` ` `// then it is a prime ` ` ` `if` `(prime[p]) ` ` ` `{ ` ` ` `// Update all multiples of p, ` ` ` `// set them to non-prime ` ` ` `for` `(` `int` `i = p * ` `2` `; i < MAX; i += p) ` ` ` `prime[i] = ` `false` `; ` ` ` `} ` ` ` `} ` `} ` ` ` `// Function to return the count of primes ` `// less than or equal to n which can be ` `// expressed as the sum of two primes ` `static` `int` `countPrimes(` `int` `n) ` `{ ` ` ` `SieveOfEratosthenes(); ` ` ` ` ` `// To store the required count ` ` ` `int` `cnt = ` `0` `; ` ` ` ` ` `for` `(` `int` `i = ` `2` `; i < n; i++) ` ` ` `{ ` ` ` `// If the integer is prime and it ` ` ` `// can be expressed as the sum of ` ` ` `// 2 and a prime number ` ` ` `if` `(prime[i] && prime[i - ` `2` `]) ` ` ` `cnt++; ` ` ` `} ` ` ` `return` `cnt; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `11` `; ` ` ` ` ` `System.out.print(countPrimes(n)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `MAX` `=` `100005` `prime ` `=` `[` `True` `for` `i ` `in` `range` `(` `MAX` `)] ` ` ` `# Function for Sieve of Eratosthenes ` `def` `SieveOfEratosthenes(): ` ` ` ` ` `# False here indicates ` ` ` `# that it is not prime ` ` ` `prime[` `0` `] ` `=` `False` ` ` `prime[` `1` `] ` `=` `False` ` ` ` ` `for` `p ` `in` `range` `(` `MAX` `): ` ` ` ` ` `if` `(p ` `*` `p > ` `MAX` `): ` ` ` `break` ` ` ` ` `# If prime[p] is not changed, ` ` ` `# then it is a prime ` ` ` `if` `(prime[p]): ` ` ` ` ` `# Update all multiples of p, ` ` ` `# set them to non-prime ` ` ` `for` `i ` `in` `range` `(` `2` `*` `p, ` `MAX` `, p): ` ` ` `prime[i] ` `=` `False` ` ` `# Function to return the count of primes ` `# less than or equal to n which can be ` `# expressed as the sum of two primes ` `def` `countPrimes(n): ` ` ` `SieveOfEratosthenes() ` ` ` ` ` `# To store the required count ` ` ` `cnt ` `=` `0` ` ` ` ` `for` `i ` `in` `range` `(` `2` `, n): ` ` ` ` ` `# If the integer is prime and it ` ` ` `# can be expressed as the sum of ` ` ` `# 2 and a prime number ` ` ` `if` `(prime[i] ` `and` `prime[i ` `-` `2` `]): ` ` ` `cnt ` `+` `=` `1` ` ` ` ` `return` `cnt ` ` ` `# Driver code ` `n ` `=` `11` ` ` `print` `(countPrimes(n)) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

` ` `// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` `static` `int` `MAX = 100005; ` `static` `bool` `[]prime = ` `new` `bool` `[MAX]; ` ` ` `// Function for Sieve of Eratosthenes ` `static` `void` `SieveOfEratosthenes() ` `{ ` ` ` `for` `(` `int` `i = 0; i < MAX; i++) ` ` ` `prime[i] = ` `true` `; ` ` ` ` ` `// false here indicates ` ` ` `// that it is not prime ` ` ` `prime[0] = ` `false` `; ` ` ` `prime[1] = ` `false` `; ` ` ` ` ` `for` `(` `int` `p = 2; p * p < MAX; p++) ` ` ` `{ ` ` ` `// If prime[p] is not changed, ` ` ` `// then it is a prime ` ` ` `if` `(prime[p]) ` ` ` `{ ` ` ` `// Update all multiples of p, ` ` ` `// set them to non-prime ` ` ` `for` `(` `int` `i = p * 2; i < MAX; i += p) ` ` ` `prime[i] = ` `false` `; ` ` ` `} ` ` ` `} ` `} ` ` ` `// Function to return the count of primes ` `// less than or equal to n which can be ` `// expressed as the sum of two primes ` `static` `int` `countPrimes(` `int` `n) ` `{ ` ` ` `SieveOfEratosthenes(); ` ` ` ` ` `// To store the required count ` ` ` `int` `cnt = 0; ` ` ` ` ` `for` `(` `int` `i = 2; i < n; i++) ` ` ` `{ ` ` ` `// If the integer is prime and it ` ` ` `// can be expressed as the sum of ` ` ` `// 2 and a prime number ` ` ` `if` `(prime[i] && prime[i - 2]) ` ` ` `cnt++; ` ` ` `} ` ` ` `return` `cnt; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `n = 11; ` ` ` ` ` `Console.Write(countPrimes(n)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

**Output:**

2

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.

## Recommended Posts:

- Count primes that can be expressed as sum of two consecutive primes and 1
- Check if an integer can be expressed as a sum of two semi-primes
- Count of primes in a given range that can be expressed as sum of perfect squares
- Find the sum of all Truncatable primes below N
- Print all safe primes below N
- Elements of Array which can be expressed as power of prime numbers
- Elements of Array which can be expressed as power of some integer to given exponent K
- Check if a prime number can be expressed as sum of two Prime Numbers
- Check if a number can be expressed as sum two abundant numbers
- Check if a number can be expressed as sum of two Perfect powers
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Check if a given number can be expressed as pair-sum of sum of first X natural numbers
- Count numbers which can be represented as sum of same parity primes
- Sum of the multiples of two numbers below N
- Count of numbers below N whose sum of prime divisors is K
- Check if a number can be expressed as a sum of consecutive numbers
- Find ways an Integer can be expressed as sum of n-th power of unique natural numbers
- Find the prime numbers which can written as sum of most consecutive primes
- Minimum numbers needed to express every integer below N as a sum
- Find the sum of all multiples of 2 and 5 below N

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.