Skip to content

Instantly share code, notes, and snippets.

@montycheese
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save montycheese/c9e4ce36df185ac3cc07 to your computer and use it in GitHub Desktop.
Save montycheese/c9e4ce36df185ac3cc07 to your computer and use it in GitHub Desktop.
Reversing an integer in PHP with pure tail recursion
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