Remember answers with memoization
Counting the ways to climb n stairs taking one or two steps repeats the same subproblems many times. Store each answer the first time it is computed and the exponential tree collapses to a straight line.
from functools import cache
@cache
def ways(n):
if n <= 1:
return 1
return ways(n - 1) + ways(n - 2)Think before coding
The last step was either one stair or two, so ways(n) = ways(n - 1) + ways(n - 2). A table filled from the bottom gives the same numbers without recursion and works for very large n. Work through the example by hand before changing the starter. Decide what should happen for an empty input, a single value and a repeated value where those cases apply. The checks use several inputs so a hard-coded answer cannot stand in for the rule.
Your task
Write climb_ways(n) returning the number of ways to climb n stairs with steps of 1 or 2. climb_ways(0) is 1. It must handle n = 5000 quickly.
The explanation opens here once every check passes.