PHP to find the specified range of palindrome number and square root is also a palindrome number of methods _php skills

Source: Internet
Author: User
Tags explode square root

This article describes the PHP to find the specified range of palindrome number and square root is also a palindrome number method. Share to everyone for your reference. Specifically as follows:

First, the request:

Gives two values x and Y, counts the number of palindrome in this interval, and requires that their square roots are also palindrome numbers. where 1<= x <= y < 10 14

Second, the solution:

<?php
error_reporting (e_all);
Ini_set ("Display_errors", 1);
Avoid timeout
set_time_limit (0);
$t 1=microtime ();
function Isplalindrome ($num) {
  $str = "$num";
  $len =strlen ($num);
  $k = Intval ($len/2) + 1;//get median for
  ($j =0; $j < $k; $j + +) {
    if ($str {$j}!= $str {$len-n $j}) { 
      return false;
    }
  } 
  return true;
}
function Showplalindrome ($min, $max) {
//Because the number of palindrome $min between $max is calculated and its square root is also a palindrome number
//So it is equivalent to sqrt ($min) ~sqrt ($max The number
//its square in the $min~ $max is also palindrome number
//$min ~ $max is a continuous positive integer, so you can shrink a lot of calculation, 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";
? >

I hope this article will help you with your PHP program design.

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.