Check whether a number is circular prime or not | Function to check if a number is prime or not . ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to check if the number is circular prime or not . ; Count digits . ; Following three lines generate the next circular permutation of a number . We move last digit to first position . ; If all the permutations are checked and we obtain original number exit from loop . ; Driver Code
< ? php function isPrime ( $ n ) { if ( $ n <= 1 ) return false ; if ( $ n <= 3 ) return true ; if ( $ n % 2 == 0 $ n % 3 == 0 ) return false ; for ( $ i = 5 ; $ i * $ i <= $ n ; $ i = $ i + 6 ) if ( $ n % $ i == 0 || $ n % ( $ i + 2 ) == 0 ) return -1 ; return true ; } function checkCircular ( $ N ) { $ count = 0 ; $ temp = $ N ; while ( $ temp ) { $ count ++ ; $ temp = ( int ) $ temp / 10 ; } $ num = $ N ; while ( isPrime ( $ num ) ) { $ rem = $ num % 10 ; $ div = ( int ) $ num / 10 ; $ num = ( pow ( 10 , $ count - 1 ) ) * $ rem + $ div ; if ( $ num == $ N ) return true ; } return -1 ; } $ N = 1193 ; if ( checkCircular ( $ N ) ) echo " Yes " , " STRNEWLINE " ; else echo " No " , " STRNEWLINE " ; ? >