Count primes that can be expressed as sum of two consecutive primes and 1 | PHP 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
< ? php $ N = 10005 ; $ isprime = array_fill ( 0 , $ N , true ) ; $ can = array_fill ( 0 , $ N , false ) ; function SieveOfEratosthenes ( ) { global $ N , $ isprime ; for ( $ p = 2 ; $ p * $ p < $ N ; $ p ++ ) { if ( $ isprime [ $ p ] == true ) { for ( $ i = $ p * $ p ; $ i < $ N ; $ i += $ p ) $ isprime [ $ i ] = false ; } } $ primes = array ( ) ; for ( $ i = 2 ; $ i < $ N ; $ i ++ ) if ( $ isprime [ $ i ] ) array_push ( $ primes , $ i ) ; return $ primes ; } function Prime_Numbers ( $ n ) { global $ N , $ can , $ isprime ; $ primes = SieveOfEratosthenes ( ) ; for ( $ i = 0 ; $ i < count ( $ primes ) - 1 ; $ i ++ ) if ( $ primes [ $ i ] + $ primes [ $ i + 1 ] + 1 < $ N ) $ can [ $ primes [ $ i ] + $ primes [ $ i + 1 ] + 1 ] = true ; $ ans = 0 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { if ( $ can [ $ i ] and $ isprime [ $ i ] ) { $ ans ++ ; } } return $ ans ; } $ n = 50 ; echo Prime_Numbers ( $ n ) ; ? >