Related Articles
Count primes that can be expressed as sum of two consecutive primes and 1
• Difficulty Level : Hard
• Last Updated : 14 Oct, 2019

Given a number N. The task is to count the number of prime numbers from 2 to N that can be expressed as a sum of two consecutive primes and 1.

Examples:

Input: N = 27
Output: 2
13 = 5 + 7 + 1 and 19 = 7 + 11 + 1 are the required prime numbers.

Input: N = 34
Output: 3
13 = 5 + 7 + 1, 19 = 7 + 11 + 1 and 31 = 13 + 17 + 1.

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

Approach: An efficient approach is to find all the primes numbers up to N using Sieve of Eratosthenes and place all the prime numbers in a vector. Now, run a simple loop and add two consecutive primes and 1 then check if this sum is also a prime. If it is then increment the count.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;``#define N 100005`` ` `// To check if a number is prime or not``bool` `isprime[N];`` ` `// To store possible numbers``bool` `can[N];`` ` `// Function to return all prime numbers``vector<``int``> SieveOfEratosthenes()``{`` ` `    ``memset``(isprime, ``true``, ``sizeof``(isprime));`` ` `    ``for` `(``int` `p = 2; p * p < N; p++) {`` ` `        ``// If prime[p] is not changed, then it is a prime``        ``if` `(isprime[p] == ``true``) {`` ` `            ``// Update all multiples of p greater than or``            ``// equal to the square of it``            ``// numbers which are multiple of p and are``            ``// less than p^2 are already been marked.``            ``for` `(``int` `i = p * p; i < N; i += p)``                ``isprime[i] = ``false``;``        ``}``    ``}`` ` `    ``vector<``int``> primes;``    ``for` `(``int` `i = 2; i < N; i++)``        ``if` `(isprime[i])``            ``primes.push_back(i);`` ` `    ``return` `primes;``}`` ` `// Function to count all possible prime numbers that can be``// expressed as the sum of two consecutive primes and one``int` `Prime_Numbers(``int` `n)``{``    ``vector<``int``> primes = SieveOfEratosthenes();`` ` `    ``// All possible prime numbers below N``    ``for` `(``int` `i = 0; i < (``int``)(primes.size()) - 1; i++)``        ``if` `(primes[i] + primes[i + 1] + 1 < N)``            ``can[primes[i] + primes[i + 1] + 1] = ``true``;`` ` `    ``int` `ans = 0;``    ``for` `(``int` `i = 2; i <= n; i++) {``        ``if` `(can[i] and isprime[i]) {``            ``ans++;``        ``}``    ``}`` ` `    ``return` `ans;``}`` ` `// Driver code``int` `main()``{``    ``int` `n = 50;``    ``cout << Prime_Numbers(n);`` ` `    ``return` `0;``}`

## Java

 `// Java implementation of the approach ``import` `java.util.*;`` ` `class` `GfG ``{`` ` `static` `int` `N = ``100005``; `` ` `// To check if a number is prime or not ``static` `boolean` `isprime[] = ``new` `boolean``[N]; `` ` `// To store possible numbers ``static` `boolean` `can[] = ``new` `boolean``[N]; `` ` `// Function to return all prime numbers ``static` `ArrayListSieveOfEratosthenes() ``{ ``     ` `    ``for``(``int` `a = ``0` `; a < isprime.length; a++)``    ``{``        ``isprime[a] = ``true``;``    ``}``    ``for` `(``int` `p = ``2``; p * p < N; p++) ``    ``{ `` ` `        ``// If prime[p] is not changed, then it is a prime ``        ``if` `(isprime[p] == ``true``)``        ``{ `` ` `            ``// Update all multiples of p greater than or ``            ``// equal to the square of it ``            ``// numbers which are multiple of p and are ``            ``// less than p^2 are already been marked. ``            ``for` `(``int` `i = p * p; i < N; i += p) ``                ``isprime[i] = ``false``; ``        ``} ``    ``} `` ` `    ``ArrayList primes = ``new` `ArrayList (); ``    ``for` `(``int` `i = ``2``; i < N; i++) ``        ``if` `(isprime[i]) ``            ``primes.add(i); `` ` `    ``return` `primes; ``} `` ` `// Function to count all possible prime numbers that can be ``// expressed as the sum of two consecutive primes and one ``static` `int` `Prime_Numbers(``int` `n) ``{ ``    ``ArrayList primes = SieveOfEratosthenes(); `` ` `    ``// All possible prime numbers below N ``    ``for` `(``int` `i = ``0``; i < (``int``)(primes.size()) - ``1``; i++) ``        ``if` `(primes.get(i) + primes.get(i + ``1``) + ``1` `< N) ``            ``can[primes.get(i) + primes.get(i + ``1``) + ``1``] = ``true``; `` ` `    ``int` `ans = ``0``; ``    ``for` `(``int` `i = ``2``; i <= n; i++) ``    ``{ ``        ``if` `(can[i] && isprime[i] == ``true``) ``        ``{ ``            ``ans++; ``        ``} ``    ``} `` ` `    ``return` `ans; ``} `` ` `// Driver code ``public` `static` `void` `main(String[] args) ``{ ``    ``int` `n = ``50``; ``    ``System.out.println(Prime_Numbers(n)); ``}``} `` ` `// This code is contributed by ``// Prerna Saini.`

## Python3

 `# Python3 implementation of the approach ``from` `math ``import` `sqrt;`` ` `N ``=` `100005``;`` ` `# To check if a number is prime or not ``isprime ``=` `[``True``] ``*` `N; `` ` `# To store possible numbers ``can ``=` `[``False``] ``*` `N; `` ` `# Function to return all prime numbers ``def` `SieveOfEratosthenes() :`` ` `    ``for` `p ``in` `range``(``2``, ``int``(sqrt(N)) ``+` `1``) :`` ` `        ``# If prime[p] is not changed, ``        ``# then it is a prime ``        ``if` `(isprime[p] ``=``=` `True``) :`` ` `            ``# Update all multiples of p greater ``            ``# than or equal to the square of it ``            ``# numbers which are multiple of p and are ``            ``# less than p^2 are already been marked. ``            ``for` `i ``in` `range``(p ``*` `p, N , p) : ``                ``isprime[i] ``=` `False``; `` ` `    ``primes ``=` `[]; ``    ``for` `i ``in` `range``(``2``, N) :``        ``if` `(isprime[i]): ``            ``primes.append(i); `` ` `    ``return` `primes; `` ` `# Function to count all possible prime numbers``# that can be expressed as the sum of two ``# consecutive primes and one ``def` `Prime_Numbers(n) : `` ` `    ``primes ``=` `SieveOfEratosthenes(); `` ` `    ``# All possible prime numbers below N ``    ``for` `i ``in` `range``(``len``(primes) ``-` `1``) :``        ``if` `(primes[i] ``+` `primes[i ``+` `1``] ``+` `1` `< N) :``            ``can[primes[i] ``+` `primes[i ``+` `1``] ``+` `1``] ``=` `True``; `` ` `    ``ans ``=` `0``; ``    ``for` `i ``in` `range``(``2``, n ``+` `1``) : ``        ``if` `(can[i] ``and` `isprime[i]) : ``            ``ans ``+``=` `1``; ``             ` `    ``return` `ans; `` ` `# Driver code ``if` `__name__ ``=``=` `"__main__"` `:`` ` `    ``n ``=` `50``; ``    ``print``(Prime_Numbers(n)); `` ` `# This code is contributed by Ryuga`

## C#

 `// C# implementation of the approach ``using` `System;``using` `System.Collections;`` ` `class` `GfG ``{`` ` `static` `int` `N = 100005; `` ` `// To check if a number is prime or not ``static` `bool``[] isprime = ``new` `bool``[N]; `` ` `// To store possible numbers ``static` `bool``[] can = ``new` `bool``[N]; `` ` `// Function to return all prime numbers ``static` `ArrayList SieveOfEratosthenes() ``{ ``     ` `    ``for``(``int` `a = 0 ; a < N; a++)``    ``{``        ``isprime[a] = ``true``;``    ``}``    ``for` `(``int` `p = 2; p * p < N; p++) ``    ``{ `` ` `        ``// If prime[p] is not changed, then it is a prime ``        ``if` `(isprime[p] == ``true``)``        ``{ `` ` `            ``// Update all multiples of p greater than or ``            ``// equal to the square of it ``            ``// numbers which are multiple of p and are ``            ``// less than p^2 are already been marked. ``            ``for` `(``int` `i = p * p; i < N; i += p) ``                ``isprime[i] = ``false``; ``        ``} ``    ``} `` ` `    ``ArrayList primes = ``new` `ArrayList(); ``    ``for` `(``int` `i = 2; i < N; i++) ``        ``if` `(isprime[i]) ``            ``primes.Add(i); `` ` `    ``return` `primes; ``} `` ` `// Function to count all possible prime numbers that can be ``// expressed as the sum of two consecutive primes and one ``static` `int` `Prime_Numbers(``int` `n) ``{ ``    ``ArrayList primes = SieveOfEratosthenes(); `` ` `    ``// All possible prime numbers below N ``    ``for` `(``int` `i = 0; i < primes.Count - 1; i++) ``        ``if` `((``int``)primes[i] + (``int``)primes[i + 1] + 1 < N) ``            ``can[(``int``)primes[i] + (``int``)primes[i + 1] + 1] = ``true``; `` ` `    ``int` `ans = 0; ``    ``for` `(``int` `i = 2; i <= n; i++) ``    ``{ ``        ``if` `(can[i] && isprime[i] == ``true``) ``        ``{ ``            ``ans++; ``        ``} ``    ``} `` ` `    ``return` `ans; ``} `` ` `// Driver code ``static` `void` `Main() ``{ ``    ``int` `n = 50; ``    ``Console.WriteLine(Prime_Numbers(n)); ``}``} `` ` `// This code is contributed by mits`

## PHP

 ``
Output:
```5
```

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up