Home > Backend Development > PHP Tutorial > PHP method to find the number of palindromes in a specified range and the square root is also a palindrome number_PHP Tutorial

PHP method to find the number of palindromes in a specified range and the square root is also a palindrome number_PHP Tutorial

WBOY
Release: 2016-07-13 10:00:31
Original
884 people have browsed it

php method to find the number of palindromes in a specified range and the square root is also a palindrome

This article mainly introduces php to find the number of palindromes in a specified range and the square root is also a palindrome The method of palindrome number, an example analysis of the technique of judging palindrome in PHP, has certain reference value, friends in need can refer to it

The example in this article describes how PHP can find the number of palindromes in a specified range and the square root is also a palindrome number. Share it with everyone for your reference. The details are as follows:

1. Requirements:

Given two values ​​​​X and Y, count the number of palindromes in this interval, and require that their square roots are also palindromes. where 1<= x <= y < 10 14

2. Solution:

?

I hope this article will be helpful to everyone’s PHP programming design. http://www.bkjia.com/PHPjc/973131.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/973131.htmlTechArticlephp method to find the number of palindromes in a specified range and the square root is also a palindrome number. This article mainly introduces php Method to find the number of palindromes in a specified range and the square root is also a palindrome number. Example analysis...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

error_reporting(E_ALL);

ini_set("display_errors", 1);

//避免超时

set_time_limit(0);

$t1=microtime();

function isPlalindrome($num){

$str="$num";

$len=strlen($num);

$k = intval($len/2) 1;//获取中间位数

for($j=0;$j<$k;$j ){

if($str{$j}!=$str{$len-1-$j}){

return false;

}

}

return true;

}

function showPlalindrome($min,$max){

//因为要计算在$min,$max间的回文数且其自身平方根也是回文数

//所以相当于求一sqrt($min)~sqrt($max)间数

//其平方在$min~$max间也是回文数

//$min~$max是连续正整数,所以可以这样缩小很多计算量,否则……

$start=sqrt($min);

$end=sqrt($max);

for($i=$start;$i<$end;$i ){

if(isPlalindrome($i) &&isPlalindrome($n=$i*$i) ){

echo $n."
";

}

}

}

showPlalindrome(1,100000000000000);

$t2=microtime();

$starttime = explode(" ",$t1);

$endtime = explode(" ",$t2);

$totaltime = $endtime[0]-$starttime[0] $endtime[1]-$starttime[1];

$timecost = sprintf("%s",$totaltime);

echo "页面运行时间: $timecost 秒";

?>

1 2

3

4

5

6 7

89 10 11 12 13 14
15
16
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
<🎜>error_reporting(E_ALL);<🎜> <🎜>ini_set("display_errors", 1);<🎜> <🎜>//Avoid timeout<🎜> <🎜>set_time_limit(0);<🎜> <🎜>$t1=microtime();<🎜> <🎜>function isPlalindrome($num){<🎜> <🎜>$str="$num";<🎜> <🎜>$len=strlen($num);<🎜> <🎜>$k = intval($len/2) 1;//Get the middle number of digits<🎜> <🎜>for($j=0;$j<$k;$j ){<🎜> <🎜>if($str{$j}!=$str{$len-1-$j}){<🎜> <🎜>return false;<🎜> <🎜>}<🎜> <🎜>}<🎜> <🎜>return true;<🎜> <🎜>}<🎜> <🎜>function showPlalindrome($min,$max){<🎜> <🎜>//Because we need to calculate the palindrome number between $min and $max and its square root is also a palindrome number<🎜> <🎜>//So it is equivalent to finding the number between sqrt($min)~sqrt($max)<🎜> <🎜>//The square is also a palindrome number between $min~$max<🎜> <🎜>//$min~$max are continuous positive integers, so this can reduce a lot of calculations, otherwise...<🎜> <🎜>$start=sqrt($min);<🎜> <🎜>$end=sqrt($max);<🎜> <🎜>for($i=$start;$i<$end;$i ){<🎜> <🎜>if(isPlalindrome($i) &&isPlalindrome($n=$i*$i) ){<🎜> <🎜>echo $n."
"; } } } showPlalindrome(1,100000000000000); $t2=microtime(); $starttime = explode(" ",$t1); $endtime = explode(" ",$t2); $totaltime = $endtime[0]-$starttime[0] $endtime[1]-$starttime[1]; $timecost = sprintf("%s",$totaltime); echo "Page running time: $timecost seconds"; ?>