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.