How to implement a palindrome algorithm in PHP
How to implement a palindrome algorithm in PHP? I believe many inexperienced people don't know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
① selects any numeric value
② flips this value (for example, select 13 to flip to 31) and adds the original value to the flip value (13: 31)
If the result of ③ addition is not palindromes, ② is returned for repeated execution. If palindromes are used, the algorithm is terminated.
For example:
13 / 31 / 44 / 44 is a palindrome, quit
199110110011121121 is a palindrome, exit
$num = 1919, 1955, 1110, 1875, 4884, 1910, 1875, 4884, 1910, 1875, 4884, 1910, 1910, 125, 646, 1884, 1884, 1947, 1884, 1947, 1888, 1888, 1888, and retrieve the numeric algorithm function huiwenshuzi ($num) {if ($num > 0) {/ / vice versa, $reNum = (int) implode (', array_reverse ($num)); $newNum = $num+$reNum If (isHuiWen ($newNum)) {/ / Export return $num+$reNum;} else {return huiwenshuzi ($newNum); / / Recursive}} else {return 'error' }} / / determine whether palindromes function isHuiWen ($str) {$str = preg_split ('/ (?)