Count prime numbers up to N that can be represented as a sum of two prime numbers | Function to store all prime numbers up to N using Sieve of Eratosthenes ; Set 0 and 1 as non - prime ; If p is prime ; Set all multiples of p as non - prime ; Function to count prime numbers up to N that can be represented as the sum of two prime numbers ; Stores all the prime numbers ; Update the prime array ; Create a dp array of size n + 1 ; Update dp [ 1 ] = 0 ; Iterate over the range [ 2 , N ] ; Add the previous count value ; Increment dp [ i ] by 1 if i and ( i - 2 ) are both prime ; Print the result ; Driver code
function SieveOfEratosthenes ( n , prime ) { prime [ 0 ] = 0 ; prime [ 1 ] = 0 ; for ( var p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == Boolean ( true ) ) { for ( var i = p * p ; i <= n ; i += p ) { prime [ i ] = Boolean ( false ) ; } } } } function countPrime ( n ) { var prime = new Array ( n + 1 ) ; var x = new Boolean ( true ) ; prime . fill ( x ) ; SieveOfEratosthenes ( n , prime ) ; var dp = new Array ( n + 1 ) ; dp . fill ( 0 ) ; dp [ 1 ] = 0 ; for ( var i = 2 ; i <= n ; i ++ ) { dp [ i ] += dp [ i - 1 ] ; if ( prime [ i ] == Boolean ( true ) && prime [ i - 2 ] == Boolean ( true ) ) { dp [ i ] ++ ; } } document . write ( dp [ n ] ) ; } var n = 6 ; countPrime ( n ) ;