Print prime numbers with prime sum of digits in an array | Function to store the primes ; Function to return the sum of digits ; Function to print additive primes ; If the number is prime ; Check if it 's digit sum is prime ; Driver code
function sieve ( maxEle , prime ) { prime [ 0 ] = prime [ 1 ] = 1 ; for ( var i = 2 ; i * i <= maxEle ; i ++ ) { if ( ! prime [ i ] ) { for ( var j = 2 * i ; j <= maxEle ; j += i ) prime [ j ] = 1 ; } } } function digitSum ( n ) { var sum = 0 ; while ( n ) { sum += n % 10 ; n = parseInt ( n / 10 ) ; } return sum ; } function printAdditivePrime ( arr , n ) { var maxEle = arr . reduce ( ( a , b ) => Math . max ( a , b ) ) ; var prime = Array ( maxEle + 1 ) . fill ( 0 ) ; sieve ( maxEle , prime ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] == 0 ) { var sum = digitSum ( arr [ i ] ) ; if ( prime [ sum ] == 0 ) document . write ( arr [ i ] + " " ) ; } } } var a = [ 2 , 4 , 6 , 11 , 12 , 18 , 7 ] ; var n = a . length ; printAdditivePrime ( a , n ) ;