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
< ? php function digitProduct ( $ n ) { $ prod = 1 ; while ( $ n ) { $ prod = $ prod * ( $ n % 10 ) ; $ n = floor ( $ n / 10 ) ; } return $ prod ; } function printMultiplicativePrimes ( $ n ) { $ prime = array_fill ( 0 , $ n + 1 , true ) ; $ prime [ 0 ] = $ prime [ 1 ] = false ; for ( $ p = 2 ; $ p * $ p <= $ n ; $ p ++ ) { if ( $ prime [ $ p ] ) { for ( $ i = $ p * 2 ; $ i <= $ n ; $ i += $ p ) $ prime [ $ i ] = false ; } } for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { if ( $ prime [ $ i ] && $ prime [ digitProduct ( $ i ) ] ) echo $ i , " ▁ " ; } } $ n = 10 ; printMultiplicativePrimes ( $ n ) ; ? >