Recursion

Problem shape

Beginner
4
8
15
16
23
42
Core idea

Multiply n by the factorial of n - 1 until the base case.

Step 1 / 5
Problem shape
Current lesson

Factorial Lab

Recursion Beginner

Factorial

Welcome to the Factorial algorithm lab. Multiply n by the factorial of n - 1 until the base case. Use it when: Use as the first example for recursive base cases and shrinking input. The time complexity is O(n), and the space complexity is O(n). The pseudocode is: fact(n) if n <= 1 return 1 return n * fact(n - 1) As you practice, track the state that changes and the condition that makes the algorithm stop.

Time

O(n)

Space

O(n)

Pseudocode

fact(n)
  if n <= 1 return 1
  return n * fact(n - 1)
Open deeper structure lesson
1.0x