How do I think about recursion?
Recursivity is not an optimal method to determine if a number is a palindrome or not. Even for strings, it's a bad application, because other methods work better.
For a number, you could just reverse the digits and then compare the two integer values:
$number = 12321; $reverse= 0; $temp = $number; while ($temp > 0) { $reverse = $reverse * 10 + $temp % 10; // % = mod, \, remainder of number divided by 10 $temp = intdiv($temp,10); // integer division or temp / 10 in c , if both values are integer } echo $reverse;
For strings, it's much faster and less memory and processor intensive to do it without recursivity, because each time the function calls itself, there's some data that has to be saved up and then restored once the function exits, and lots of functions nested inside each other , you get all those extra "save and restore at exit" stuff that uses resources.
For strings you just determine the string length, divide it by 2 and then go from the edges to the center and check characters.
<?php $text = "abcdefgfedcca"; var_dump(is_palindrome($text)); var_dump(is_palindrome2($text)); function is_palindrome($text){ $len = strlen($text); $half = intdiv($len,2); for ($i=0;$i<$len;$i++) { $a = substr($text,$i,1); $b = substr($text,$len-1-$i,1); // because strings start from 0 if ($a!=$b) return FALSE; } return TRUE; } // using reverse string function in php //(could be slower because in previous function we exit at the first bad character) // this one has to reverse whole string before we check function is_palindrome2($text) { return strrev($text)==$text ? true : false; } ?>
A good application of recursivity could be for example sorting a big array of numbers.
You have 1000 numbers, you start the function with two parameters, the start point and end point in array.
The function can then call itself two times, with half the array, and so on until the length is low enough it's not worth dividing further.
Then when you come out of recursivity, merge the two strings, and return and so on ...
Here's an example of such usage :
(json_encode is just used to output the array of numbers on the screen in a nice way, it just does "[ number 1 , number 2 , .... , number n ] so ignore it :
<?php $numbers = []; for ($i=0;$i<24;$i++) { $numbers[$i]=mt_rand(0,1000); } echo json_encode($numbers)."\n"; $result = sort_recursive($numbers,0,23); function sort_recursive(&$numbers,$from,$to,$level=0) { echo "level=$level, from=$from, to=$to\n"; $count = $to-$from; if ($count<8) { // too few numbers to recursive it, so sort it $sorted=false; while ($sorted==false) { $sorted=true; for ($i=$from;$i<$to;$i++) { if ($numbers[$i]>$numbers[$i+1]) { $temp=$numbers[$i]; $numbers[$i]=$numbers[$i+1]; $numbers[$i+1]=$temp; $sorted=false; } } } echo json_encode($numbers)."\n"; return ; } else { //split this interval into two smaller chunks, // call myself two times and then merge the two sorted intervals $split = intdiv($count,2); $result = sort_recursive($numbers,$from,$from+$split,$level+1); $result = sort_recursive($numbers,$from+$split+1,$to,$level+1); // when we return the two segments are sorted but the whole segment. // may not be sorted ex s1 = 1,5,7 s2 = 2,3,10 // we need to merge them ex s = 1,2,3,5,7,10 $i=$from; $j=$from+$split+1; //can be done without extra temp arrays but easier to understand this way $a = []; $b = []; // array_push places the value into array for ($i=$from;$i<$j;$i++) array_push($a,$numbers[$i]); for ($i=$j;$i<=$to;$i++) array_push($b,$numbers[$i]); for ($k=0;$k<=$count;$k++){ if ((count($a)>0) && (count($b)>0)) { if ($a[0]<=$b[0]) { // pulls first value from a and shrinks the a array $numbers[$from+$k] = array_shift($a); } else { // pulls first value from b and shrinks the b array $numbers[$from+$k] = array_shift($b); } } else { // at some point one of the arrays may be empty so we jus // copy what's left from the other non empty array if (count($a)>0) $numbers[$from+$k] = array_shift($a); if (count($b)>0) $numbers[$from+$k] = array_shift($b); } } echo "merge $from $to ".json_encode($numbers)."\n"; } } ?>
and here's the output with 24 random numbers :
[859,516,465,276,36,148,695,473,320,87,839,725,165,616,538,534,171,93,329,744,676,447,755,542] level=0, from=0, to=23 level=1, from=0, to=11 level=2, from=0, to=5 [36,148,276,465,516,859,695,473,320,87,839,725,165,616,538,534,171,93,329,744,676,447,755,542] level=2, from=6, to=11 [36,148,276,465,516,859,87,320,473,695,725,839,165,616,538,534,171,93,329,744,676,447,755,542] merge 0 11 [36,87,148,276,320,465,473,516,695,725,839,859,165,616,538,534,171,93,329,744,676,447,755,542] level=1, from=12, to=23 level=2, from=12, to=17 [36,87,148,276,320,465,473,516,695,725,839,859,93,165,171,534,538,616,329,744,676,447,755,542] level=2, from=18, to=23 [36,87,148,276,320,465,473,516,695,725,839,859,93,165,171,534,538,616,329,447,542,676,744,755] merge 12 23 [36,87,148,276,320,465,473,516,695,725,839,859,93,165,171,329,447,534,538,542,616,676,744,755] merge 0 23 [36,87,93,148,165,171,276,320,329,447,465,473,516,534,538,542,616,676,695,725,744,755,839,859]
You can see the function starts at level 0 with from = 0 and to = 23 , so it knows there's more than 8 values to sort, so it calls itself two times. Once it's with level 1 from 0 to 11, and the second time is level 1 from 12 to 23.
so we go first in level 1 from 0 to 11 and that function also realizes it's more than 8 numbers, so it calls itself again twice, and we're at level 2 from 0 to 5 , and level 2 from 6 to 11
The function level 2 from 0 to 5 notices finds there's only 6 numbers to sort so no longer calls itself, it just sorts the numbers and returns.
The function level 2 from 6 to 11 also notices there's less than 8 numbers to sort, so it just sorts the array and returns.
Now the control goes back to the previous function (level 1, from 0 to 11) and the function now knows there's two segments that are each sorted : from 0 to 5 and from 6 to 11, so it merges them and returns.
Now the control goes back to the previous function, which calls sort with the 2nd segment, when that function returns it merges the two segments and so on...

Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now