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
using System ; using System . Collections ; class GfG { static int N = 100005 ; static bool [ ] isprime = new bool [ N ] ; static bool [ ] can = new bool [ N ] ; static ArrayList SieveOfEratosthenes ( ) { for ( int a = 0 ; a < N ; a ++ ) { isprime [ a ] = true ; } for ( int p = 2 ; p * p < N ; p ++ ) { if ( isprime [ p ] == true ) { 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 ; } static int Prime_Numbers ( int n ) { ArrayList primes = SieveOfEratosthenes ( ) ; 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 ; } static void Main ( ) { int n = 50 ; Console . WriteLine ( Prime_Numbers ( n ) ) ; } }