Print all multiplicative primes <= N | Function to return the digit product of n ; Function to print all multiplicative primes <= n ; Create a boolean array " prime [ 0 . . n + 1 ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; If i is prime and its digit sum is also prime i . e . i is a multiplicative prime ; Driver code
function digitProduct ( n ) { let prod = 1 ; while ( n > 0 ) { prod = prod * ( n % 10 ) ; n = Math . floor ( n / 10 ) ; } return prod ; } function printMultiplicativePrimes ( n ) { let prime = new Array ( n + 1 ) ; for ( let i = 0 ; i <= n ; i ++ ) prime [ i ] = true ; prime [ 0 ] = prime [ 1 ] = false ; for ( let p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] ) { for ( let i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } for ( let i = 2 ; i <= n ; i ++ ) { if ( prime [ i ] && prime [ digitProduct ( i ) ] ) document . write ( i + " " ) ; } } let n = 10 ; printMultiplicativePrimes ( n ) ;