Count primes that can be expressed as sum of two consecutive primes and 1 | C ++ implementation of the approach ; To check if a number is prime or not ; To store possible numbers ; Function to return all prime numbers ; If prime [ p ] is not changed , then it is a prime ; 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 . ; Function to count all possible prime numbers that can be expressed as the sum of two consecutive primes and one ; All possible prime numbers below N ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  100005 NEW_LINE bool isprime [ N ] ; bool can [ N ] ; vector < int > SieveOfEratosthenes ( ) { memset ( isprime , true , sizeof ( isprime ) ) ; for ( int p = 2 ; p * p < N ; p ++ ) { if ( isprime [ p ] == true ) { 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 ; } int Prime_Numbers ( int n ) { vector < int > primes = SieveOfEratosthenes ( ) ; 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 ; } int main ( ) { int n = 50 ; cout << Prime_Numbers ( n ) ; return 0 ; }