PHP to find out the range of palindrome number and square root is also a palindrome number method

Source: Internet
Author: User
Tags explode
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.