Last active
April 19, 2020 14:24
-
-
Save colibie/a004a15fa2082c600d6739456fe73cb8 to your computer and use it in GitHub Desktop.
An analysis of recursive factorial
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 fact(n) { | |
if (n == 1) return n; // base case | |
return n * fact(n - 1); // recursive call | |
} | |
// fact(5) analysis - output = 120 | |
fact(5) | |
5 * fact(4) | |
5 * (4 * fact(3)) | |
5 * (4 * (3 * fact(2))) | |
5 * (4 * (3 * (2 * fact(1)))) | |
5 * (4 * (3 * (2 * 1))) | |
5 * (4 * (3 * 2)) | |
5 * (4 * 6) | |
5 * 24 | |
120 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment