How does PHP find the number of palindrome in the range and the square root is a palindrome number? This paper mainly introduces PHP to find out the number of palindrome in the specified range and the square root is also a palindrome number method, through the example analysis of PHP judgment palindrome skills. We hope to help you.
In this paper, we describe the method that PHP finds the number of palindrome in the specified range and the square root is the palindrome number. Share to everyone for your reference. Specific as follows:
First, requirements:
Two values x and Y are given, and the number of palindrome in this interval is counted, and the square root of them is also called palindrome number. where 1<= x <= y < 10 14
Second, the solution:
<?phperror_reporting (E_all); Ini_set ("Display_errors", 1);//Avoid timeouts set_time_limit (0); $t 1=microtime (); function Isplalindrome ($num) { $str = "$num"; $len =strlen ($num); $k = Intval ($len/2) + 1;//gets the median for ($j =0; $j < $k; $j + +) { if ($str {$j}!= $str {$len-$j}) { return false; } } return true;} function Showplalindrome ($min, $max) {//Because to calculate in $min, the number of palindrome in the $max and its own square root is also a palindrome number//So the equivalent of seeking a sqrt ($min) ~sqrt ($max) between the number//its square in $ Min~ $max is also a palindrome number//$min ~ $max is a continuous positive integer, so you can reduce a lot of computational capacity, otherwise ... $start =sqrt ($min); $end =sqrt ($max); for ($i = $start; $i < $end; $i + +) { if (isplalindrome ($i) &&isplalindrome ($n = $i * $i)) { echo $n. "< Br/> ";}} } Showplalindrome (1,100000000000000), $t 2=microtime (), $starttime = Explode ("", $t 1); $endtime = Explode ("", $t 2); $ TotalTime = $endtime [0]-$starttime [0]+ $endtime [1]-$starttime [1]; $timecost = sprintf ("%s", $totaltime); echo "page run Time: $ Timecost seconds ";? >
Related recommendations:
A small comparison of PHP function execution Efficiency _php Tutorial
PHP algorithm split array, without Array_chunk () _php Tutorial
PHP Troubleshooting Utf-8 and gb2312 encoding conversion issues _php Tutorial