Skip to content

Instantly share code, notes, and snippets.

@colibie
Last active April 19, 2020 14:24
Show Gist options
  • Save colibie/a004a15fa2082c600d6739456fe73cb8 to your computer and use it in GitHub Desktop.
Save colibie/a004a15fa2082c600d6739456fe73cb8 to your computer and use it in GitHub Desktop.
An analysis of recursive factorial
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