Count primes that can be expressed as sum of two consecutive primes and 1 | Python3 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
from math import sqrt ; NEW_LINE N = 100005 ; NEW_LINE isprime = [ True ] * N ; NEW_LINE can = [ False ] * N ; NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT for p in range ( 2 , int ( sqrt ( N ) ) + 1 ) : NEW_LINE INDENT if ( isprime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * p , N , p ) : NEW_LINE INDENT isprime [ i ] = False ; NEW_LINE DEDENT DEDENT DEDENT primes = [ ] ; NEW_LINE for i in range ( 2 , N ) : NEW_LINE INDENT if ( isprime [ i ] ) : NEW_LINE INDENT primes . append ( i ) ; NEW_LINE DEDENT DEDENT return primes ; NEW_LINE DEDENT def Prime_Numbers ( n ) : NEW_LINE INDENT primes = SieveOfEratosthenes ( ) ; NEW_LINE for i in range ( len ( primes ) - 1 ) : NEW_LINE INDENT if ( primes [ i ] + primes [ i + 1 ] + 1 < N ) : NEW_LINE INDENT can [ primes [ i ] + primes [ i + 1 ] + 1 ] = True ; NEW_LINE DEDENT DEDENT ans = 0 ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( can [ i ] and isprime [ i ] ) : NEW_LINE INDENT ans += 1 ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 50 ; NEW_LINE print ( Prime_Numbers ( n ) ) ; NEW_LINE DEDENT