Palindromic Primes | A function that returns true only if num contains one digit ; comparison operation is faster than division operation . So using following instead of " return ▁ num ▁ / ▁ 10 ▁ = = ▁ 0 ; " ; A recursive function to find out whether num is palindrome or not . Initially , dupNum contains address of a copy of num . ; Base case ( needed for recursion termination ) : This statement / mainly compares the first digit with the last digit ; This is the key line in this method . Note that all recursive / calls have a separate copy of num , but they all share same copy of dupNum . We divide num while moving up the recursion tree ; The following statements are executed when we move up the recursion call tree ; At this point , if num % 10 contains ith digit from beginning , then ( dupNum ) % 10 contains ith digit from end ; The main function that uses recursive function isPalUtil ( ) to find out whether num is palindrome or not ; If num is negative , make it positive ; Create a separate copy of num , so that modifications made to address dupNum don 't change the input number.  $dupNum = $num; dupNum = num ; Function to generate all primes and checking whether number is palindromic or not ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . 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 ; Print all palindromic prime numbers ; checking whether the given number is prime palindromic or not ; Driver Code
< ? php function oneDigit ( $ num ) { return ( $ num >= 0 && $ num < 10 ) ; } function isPalUtil ( $ num , $ dupNum ) { if ( oneDigit ( $ num ) ) return ( $ num == ( $ dupNum ) % 10 ) ; if ( ! isPalUtil ( ( int ) ( $ num / 10 ) , $ dupNum ) ) return false ; $ dupNum = ( int ) ( $ dupNum / 10 ) ; return ( $ num % 10 == ( $ dupNum ) % 10 ) ; } function isPal ( $ num ) { if ( $ num < 0 ) $ num = - $ num ; return isPalUtil ( $ num , $ dupNum ) ; } function printPalPrimesLessThanN ( $ n ) { $ prime = array_fill ( 0 , $ n + 1 , true ) ; for ( $ p = 2 ; $ p * $ p <= $ n ; $ p ++ ) { if ( $ prime [ $ p ] ) { for ( $ i = $ p * 2 ; $ i <= $ n ; $ i += $ p ) { $ prime [ $ i ] = false ; } } } for ( $ p = 2 ; $ p <= $ n ; $ p ++ ) { if ( $ prime [ $ p ] && isPal ( $ p ) ) { print ( $ p . " " ) ; } } } $ n = 100 ; print ( " Palindromic ▁ primes ▁ smaller ▁ " . " than ▁ or ▁ equal ▁ to ▁ " . $ n . " ▁ are ▁ : STRNEWLINE " ) ; printPalPrimesLessThanN ( $ n ) ; ? >