Check whether a number has exactly three distinct factors or not | Utility function to check whether 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 whether given number has three distinct factors or not ; Find square root of number ; Check whether number is perfect square or not ; If number is perfect square , check whether square root is prime or not ; 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 false ; return true ; } function isThreeDisctFactors ( $ n ) { $ sq = sqrt ( $ n ) ; if ( $ sq * $ sq != $ n ) return false ; return isPrime ( $ sq ) ? true : false ; } $ num = 9 ; if ( isThreeDisctFactors ( $ num ) ) echo " Yes STRNEWLINE " ; else echo " No STRNEWLINE " ; $ num = 15 ; if ( isThreeDisctFactors ( $ num ) ) echo " Yes STRNEWLINE " ; else echo " No STRNEWLINE " ; $ num = 12397923568441 ; if ( isThreeDisctFactors ( $ num ) ) echo " Yes STRNEWLINE " ; else echo " No STRNEWLINE " ; ? >