Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
A staircase has n steps. You climb one step or two steps at a time. In how many different ways can you reach the top? Write a recursive function with a memory, and find the answer for 1, 2, 3, 4, 5 and 30 steps.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. Ask the four questions.
n - 1) or two steps (from step n - 2). So the ways to reach n are the ways to reach n - 1, plus the ways to reach n - 2.This is the Fibonacci rule, with different starting values. So it needs a memory, or it would be far too slow for 30.
Check. For 3 steps: (1,1,1), (1,2), (2,1), which is 3 ways. For 4: 5. For 5: 8.
Answer: 1, 2, 3, 5, 8 and 1 346 269 ways.
Log in to join the discussion.
Loading…
With Standard, you also open 10 more problems, 2 Python labs and 1 extra course. Everything you have used here stays free, always.