Last active
August 29, 2015 14:20
-
-
Save montycheese/c9e4ce36df185ac3cc07 to your computer and use it in GitHub Desktop.
Reversing an integer in PHP with pure tail recursion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function reverseInt($n){ | |
return reverseIntAcc(0, $n); | |
} | |
function reverseIntAcc($acc, $n){ | |
if ($n == 0){ | |
return $acc; | |
} | |
$acc *= 10; | |
return reverseIntAcc($acc + ($n % 10) , floor($n / 10)); | |
} | |
echo reverseInt(23579); //97532 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment