Start free

Advanced · Recursion · 166 of 264

Climbing stairs

You climb a staircase taking 1, 2 or 3 steps at a time. In how many different ways can you reach the top of n steps? Use memoisation.

Input: n (1 to 60). Output: the number of ways.

Examples

Input
3
Output
4
Input
1
Output
1
Input
4
Output
7

2 more tests check other inputs when you press Check.

Stuck? The idea behind recursion

A recursive function calls itself on a smaller version of the problem, until it reaches a case small enough to answer directly (the base case).

Trust the smaller call to be right, and only work out how to build this answer from it.

def factorial(n):
    if n == 0:
        return 1
    return n * factorial(n - 1)

print(factorial(5))

Watch for: Without a base case, or if the problem doesn't get smaller, it never stops: Python gives up with a RecursionError.

All the recursion exercises
Your code
Opening the code editor…
Python 3.14 in your browser
Output
Run shows what your program prints. Check tests it on every example and the hidden tests.

More recursion exercises