Last active
September 26, 2018 16:25
-
-
Save belushkin/62beded835e0e4b0641432fdb8edac21 to your computer and use it in GitHub Desktop.
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
<?php | |
// Regular recursion | |
function factorial ($n) | |
{ | |
if ($n == 1 ) { | |
return 1; | |
} | |
return $n * factorial($n-1); | |
} | |
// Tail recursion | |
function tail_factorial($n) | |
{ | |
function loop($acc, $n) | |
{ | |
if ($n == 0) { | |
return $acc; | |
} else { | |
return loop($acc * $n, $n - 1); | |
} | |
} | |
return loop(1, $n); | |
} | |
echo factorial(100), "\n"; | |
echo tail_factorial(100), "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment