How to find the number of possible ways to climb the staircase

613 Views Asked by At

You are going to climb up a staircase that has an n number of stairs, starting from bottom. In each step, you can only move up either one or two stairs.

Note that, as an example, after reaching the 3rd or 4th stairs, you can climb up to the 5th stair in 2 ways;

 I. move up 1 stair from the 4th stair, and

II. move up 2 stairs from the 3rd stair.

Develop a Python program to take an integer n as input (0 < n < 120) and display the number of ways you can climb up to the nth stair. You may handle unexpected inputs appropriately.

Input: Single integer n (0

Output: Single integer

Example:

Case 1:

Input:

5 Output:

8 Case 2:

Input:

9 Output:

55

Above is a question from one of my computer assignments. I don’t understand how to calculate the number of possible ways. To start coding I need a solution to the problem. How do I find the number of possible ways to climb the staircase.

1

There are 1 best solutions below

9
On BEST ANSWER

Say input$=1$, in this case output$=1$.

Now say input$=2$, in this case there is the answer of $($input$=1)$ and then another step and we have also the answer of taking $2$ steps(so $2$)

Now take input$=n$ in this case we have that output is: $($output$=n-1)$ with step of $1$ at the end and $($output$=n-2)$ with step of $2$ at the end

This is the same as the $n+1$ Fibonacci number


To make it clearer:

If I want to climb $10$ stairs. In this case I can climb $9$ and then climb a single stairs and I can also climb $8$ and then climb 2 stairs. So the number of ways to get to $10$ is the number of ways to get to $9$ plus the number of ways to get to $8$. And this is the same for all $n>2$.