Count primes that can be expressed as sum of two consecutive primes and 1 | JavaScript 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
let N = 10005 ; let isprime = new Array ( N ) . fill ( true ) ; let can = new Array ( N ) . fill ( false ) ; function SieveOfEratosthenes ( ) { for ( let p = 2 ; p * p < N ; p ++ ) { if ( isprime [ p ] == true ) { for ( let i = p * p ; i < N ; i += p ) isprime [ i ] = false ; } } let primes = new Array ( ) ; for ( let i = 2 ; i < N ; i ++ ) if ( isprime [ i ] ) primes . push ( i ) ; return primes ; } function Prime_Numbers ( n ) { let primes = SieveOfEratosthenes ( ) ; for ( let i = 0 ; i < primes . length - 1 ; i ++ ) if ( primes [ i ] + primes [ i + 1 ] + 1 < N ) can [ primes [ i ] + primes [ i + 1 ] + 1 ] = true ; let ans = 0 ; for ( let i = 2 ; i <= n ; i ++ ) { if ( can [ i ] && isprime [ i ] ) { ans ++ ; } } return ans ; } let n = 50 ; document . write ( Prime_Numbers ( n ) ) ;