Ma · Mathematics

How Many Ways Can You Climb n Stairs Using 2 or 3 at a Time?

Published By the 9to5Science editorial team

At a glance

  1. 1. Define the count

    Let a_n count routes to stair n when every move covers exactly 2 or 3 stairs.

  2. 2. Split by last move

    A route ends with a 2-stair move or a 3-stair move, giving a_n = a_{n-2} + a_{n-3}.

  3. 3. Set initial values

    Use a_0 = 1 for the empty route, and a_1 = 0 because one stair cannot be reached.

  4. 4. Count by move totals

    For each possible number of 3-stair moves, count the arrangements of all 2- and 3-stair moves.

Suppose you climb a staircase of nn stairs, and each move must cover either 2 stairs or 3 stairs. The order of the moves matters: taking a 2-step move followed by a 3-step move is a different route from taking a 3-step move followed by a 2-step move.

Let ana_n be the number of valid routes to stair nn. The recurrence follows by sorting routes according to their final move. You can also get an explicit finite sum by choosing how many 3-step moves a route contains.

The recurrence and initial values

A route to stair nn must finish in one of two ways:

  • Its last move covers 2 stairs, so the earlier part is a route to stair n−2n-2.
  • Its last move covers 3 stairs, so the earlier part is a route to stair n−3n-3.

These two cases do not overlap, so for n≥3n\ge 3:

an=an−2+an−3.a_n=a_{n-2}+a_{n-3}.

Use the following starting values:

a0=1,a1=0,a2=1.a_0=1,\qquad a_1=0,\qquad a_2=1.

The value a0=1a_0=1 counts the empty route: doing no moves is one way to reach the starting point. This convention makes the recurrence work at n=3n=3, giving a3=a1+a0=1a_3=a_1+a_0=1. There is no route to stair 1, while stair 2 can be reached with one 2-stair move.

The first values are:

nn012345678
ana_n101112234

An explicit formula using binomial coefficients

Suppose a route contains jj moves of 3 stairs. Those moves cover 3j3j stairs, so the number of 2-stair moves must be

i=n−3j2.i=\frac{n-3j}{2}.

This is possible only when n−3jn-3j is nonnegative and even. Once the route has ii moves of 2 stairs and jj moves of 3 stairs, it contains i+ji+j moves in total. Choose which jj positions contain the 3-stair moves; the remaining positions are 2-stair moves. There are (i+jj)\binom{i+j}{j} such arrangements.

Therefore,

an=∑0≤j≤⌊n/3⌋n−3j is even((n−3j)/2+jj).a_n=\sum_{\substack{0\le j\le \lfloor n/3\rfloor\\ n-3j\text{ is even}}}\binom{(n-3j)/2+j}{j}.

This finite sum gives the count directly from nn, without generating all earlier values. For n=0n=0, the sum includes j=0j=0 and gives (00)=1\binom{0}{0}=1, consistent with the empty-route convention.

Worked example: 8 stairs

For n=8n=8, the possible values of jj, the number of 3-stair moves, must make 8−3j8-3j nonnegative and even. This leaves j=0j=0 and j=2j=2.

  • If j=0j=0, there are four 2-stair moves. They have one arrangement: (40)=1\binom{4}{0}=1.
  • If j=2j=2, there is one 2-stair move and two 3-stair moves. The three moves can be ordered in (32)=3\binom{3}{2}=3 ways.

Thus,

a8=(40)+(32)=1+3=4.a_8=\binom{4}{0}+\binom{3}{2}=1+3=4.

The four routes are 2+2+2+22+2+2+2, 2+3+32+3+3, 3+2+33+2+3, and 3+3+23+3+2.

Generating function and characteristic equation

The ordinary generating function is

A(x)=∑n≥0anxn=11−x2−x3.A(x)=\sum_{n\ge 0}a_nx^n=\frac{1}{1-x^2-x^3}.

The denominator records the permitted move lengths: after any partial route, the next move contributes either x2x^2 or x3x^3. Equivalently, the recurrence has characteristic equation

r3−r−1=0.r^3-r-1=0.

If an exponential-root form is useful, let r1,r2,r3r_1,r_2,r_3 be the three roots of this equation. Then

an=∑k=13rkn+23rk2−1.a_n=\sum_{k=1}^{3}\frac{r_k^{n+2}}{3r_k^2-1}.

The roots include a complex-conjugate pair, but their contributions combine to give the real integer count. The binomial sum is often the more practical explicit formula for this particular counting problem.

Common mistakes

  • Allowing a 1-stair move: That is a different problem. If moves of 1, 2, or 3 stairs are allowed, the recurrence becomes an=an−1+an−2+an−3a_n=a_{n-1}+a_{n-2}+a_{n-3}, with different initial conditions.
  • Counting move totals but not their order: A route with one 2-stair move and two 3-stair moves has three arrangements, not one.
  • Using the wrong initial value at zero: Taking a0=0a_0=0 breaks the recurrence at n=3n=3. The empty route is counted as one way to cover zero stairs.
  • Including impossible values of jj: In the sum, n−3jn-3j must be both nonnegative and even; otherwise the required number of 2-stair moves is not a nonnegative integer.

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.