On this page
Suppose you climb a staircase of 4 steps, taking 1 or 2 steps at a time. How many different ways are there? It is one case of a bigger question: in how many ordered ways can a number be written as a sum? Each answer is a short formula that comes from one simple picture.
What is a composition of an integer?
A composition of n is a way of writing n as the sum of a sequence of positive integers, where order matters (Wikipedia: Composition (combinatorics))). So 1 + 3 and 3 + 1 are two different compositions of 4, but the same partition of 4. That is why compositions are also called ordered partitions (OEIS A011782).
The difference is large even for small numbers: the same Wikipedia article lists all 16 compositions of 5 next to the 7 partitions of 5. For the unordered version, read our guide to integer partitions and how to count them.
Why are there 2^(n−1) compositions of n?
Write n as a row of n stars. Between neighbouring stars there are n − 1 gaps. In each gap you either place a cut or you do not. The cuts split the row into blocks, and the block sizes, read left to right, give a composition.
Every pattern of cuts gives a different composition, and every composition comes from exactly one pattern. With two choices in each of n − 1 gaps, there are 2^(n−1) compositions of n (Wikipedia); the counts 1, 2, 4, 8, 16 for n = 1 to 5 appear in OEIS A011782, described there as the number of compositions of n).
Worked example for n = 4, with 3 gaps and 2^3 = 8 cut patterns:
no cuts: 4
one cut: 1 + 3, 2 + 2, 3 + 1
two cuts: 1 + 1 + 2, 1 + 2 + 1, 2 + 1 + 1
three cuts: 1 + 1 + 1 + 1
This matching of cut patterns to compositions is a bijection, the same tool as in our post on counting with a bijection.
How many compositions of n have exactly k parts?
A composition with k parts needs exactly k − 1 cuts, chosen from the n − 1 gaps. So the number of compositions of n into k parts is C(n−1, k−1) (Wikipedia)). Adding this over every k from 1 to n gives back 2^(n−1).
This is stars and bars with positive parts. The number of solutions of a + b + c = 10 in positive integers is C(9, 2) = 36. If zero is allowed, the same article gives the weak-composition count C(n + k − 1, k − 1), here C(12, 2) = 66.
For n = 6 and k = 3, C(5, 2) = 10. Check by listing: 1+1+4, 1+4+1, 4+1+1, the six orders of 1+2+3, and 2+2+2.
Why do compositions into 1s and 2s give Fibonacci numbers?
Look at the last part. If it is 1, what comes before is any composition of n − 1 into 1s and 2s. If it is 2, what comes before is one of n − 2. So the count c(n) obeys c(n) = c(n−1) + c(n−2), with c(1) = 1 and c(2) = 2. That gives 1, 2, 3, 5, 8, 13, the Fibonacci numbers shifted by one: c(n) = F(n+1).
The Fibonacci entry OEIS A000045 states it this way: F(n) is the number of compositions of n − 1 with no part greater than 2. For the 4-step staircase, the 5 ways are 1+1+1+1, 1+1+2, 1+2+1, 2+1+1 and 2+2. A 10-step staircase has F(11) = 89 ways.
Count the same set by the number of 2s and you get 5 = C(4, 0) + C(3, 1) + C(2, 2), the diagonal sums explained in Fibonacci numbers in Pascal's triangle.
How many compositions use only odd parts?
Exactly F(n). OEIS A000045 lists this too, with the 8 odd-part compositions of 6 as its example: 1+1+1+1+1+1, 1+1+1+3, 1+1+3+1, 1+3+1+1, 3+1+1+1, 3+3, 1+5 and 5+1.
The proof is the same last-part trick. If the last part is 1, remove it and you have an odd-part composition of n − 1. If the last part is 3 or more, subtract 2 from it; it stays odd, and you have an odd-part composition of n − 2. Both steps can be undone, so o(n) = o(n−1) + o(n−2), starting from o(1) = o(2) = 1.
A five-step check before you count
Does order matter? If yes, compositions; if no, partitions.
Must parts be at least 1, or can they be 0? Use C(n−1, k−1) or C(n+k−1, k−1).
Is the number of parts fixed? If not, the total is 2^(n−1).
Are parts restricted, such as only 1s and 2s or only odd numbers? Condition on the last part and write a recurrence.
Test your formula by listing every case for n = 4 or 5 by hand.
For practice, the Fermi Problem Library has a step-by-step solver that explains the reasoning. If stars and bars still feels shaky, start with the Fermi Concept Library.



