EncounterActivity 1 of 9
Encounter
How many dolls are inside a set of nesting dolls? A simple rule answers it:
- If the doll is solid (nothing inside), the answer is 1. This is the base case.
- Otherwise, the answer is 1 + (the number of dolls inside the next doll). This is the recursive case: the rule uses itself, but on a smaller doll.
Factorial works the same way: , and . In Lean:
def fact : Nat → Nat
| 0 => 1
| n + 1 => (n + 1) * fact n