The link between n-Bounded Compositions and the extended Fibonacci Sequence

During last year Advent of Code, I ended up discovering a beautiful relation between bounded integer compositions and the generalized Fibonacci sequence. Probably it is an already known relation, but the fact that it popped up in a totally unrelated problem is a remarkable example on how number theory properties may pop up in the most unexpected places.

Let’s start with an example.

What is a Integer Composition

Imagine you are the bottom of a staircase with 4 steps and I ask you: in how many ways can you climb the stairs?

How many different ways to climb the stairs we have?
Figure 1. How many different ways to climb the stairs we have?

The trivial climbing patter is going step by step: you climb one step, then another one, then another one, and finally the final one. We can represent this case in this way:

1,1,1,1\langle 1,1,1,1 \rangle

However, there are more possibilities. For instance, you could do the first two steps with a single step, and then continue as usual.

2,1,1\langle 2,1,1 \rangle

Or we can climb 1 step, 2 steps and then the final step.

1,2,1\langle 1,2,1 \rangle
An example of two different climbing patterns.
Figure 2. An example of two different climbing patterns.

Now, imagine for a moment that you have very long legs (infinitely long, to be precise) and you can do any number of stairs with a single step. In how many ways you can climb a staircase with N steps?

Well… this is a just an hypotetical scenario after all…
Figure 3. Well… this is a just an hypotetical scenario after all…

It is clear that the problem is equivalent to find the number of ways we can sum to NN using any number of integers. Note that the order matters: 1+2+11+2+1 is different from 2+1+12+1+1.

In this scenario, the solution is 2N12^{N-1} different “staircase climbing patterns”. This is easy to prove. In fact, for NN, we can write a “template” covering every pattern in this way:

111N\langle \overbrace{1 \square 1 \square \dots \square 1}^N \rangle

We can see that there are N1N-1 boxes and we can get every possibility by choosing for each box a “++” (plus) or a “,,” (comma). Just find the possible combinations of “plus” and “commas” and you get the solution.

What is a n-Bounded Composition

Unfortunately, in reality we haven’t infinitely long legs. There is a maximum amount of stairs we can do with a single step.

Suppose that we can do at most 3 stairs with a single step. Now the number we are looking for is different. In our 4-stairs example, for instance, the 4\langle 4 \rangle solution is no longer valid because we cannot climb 4 stairs at once.

It is clear that the number is smaller than the total number of compositions of N. But what is this number? In the N=4N=4 case, the solution is easy: just remove the only invalid solution 4\langle 4 \rangle. But for larger numbers? What about N=27N=27?

We call these Bounded Compositions. In particular, if the maximum number we can have is nn, we call these n-Bounded Compositions of NN.

The Number of 3-Limited Compositions is the Tribonacci Sequence

In the rest of the article I will show you that the number of 3-Bounded Compositions of NN is exactly equal to the NthN-th Tribonacci number.

C3(N)=Tribonacci(N)|C_3(N)| = Tribonacci(N)

A Constructive Proof

We start by manually looking for the number of compositions of basic integers.

We define that C3(0)=1|C_3(0)| = 1. It makes sense. There is only one way to sum integers to 0: doing nothing.

For N=1N=1 we trivially have that C3(1)=1|C_3(1)| = 1.

For N=2N=2 we have 2 compositions: 1,1\langle 1,1 \rangle and 2\langle 2 \rangle. So C3(2)=2|C_3(2)| = 2.

For N=3N=3 we have 4 compositions: 3\langle 3 \rangle, 2,1\langle 2,1 \rangle, 1,2\langle 1,2 \rangle and 1,1,1\langle 1,1,1 \rangle. So C3(3)=4|C_3(3)| = 4.

Up to this point there is no difference with the unconstrained compositions. Let’s look at N=4N=4. To construct the set of compositions let’s start from the one composed by only ones: 1,1,1,1\langle 1,1,1,1 \rangle. Now we can split this in two parts: the first 3 number 1s and the last 1. For the first set, we can list the 3 compositions of N=3N=3.

C3(3)C_3(3)One more 11
1,1,1\langle 1,1,111 \rangle
2,1\langle 2,111 \rangle
1,2\langle 1,211 \rangle
3\langle 311 \rangle

At this point we can already see that this tables shows 4 valid compositions of N=4N=4.

C3(4)C_3(4) based on C3(3)C_3(3)
1,1,1,1\langle 1,1,1,1 \rangle
2,1,1\langle 2,1,1 \rangle
1,2,1\langle 1,2,1 \rangle
3,1\langle 3,1 \rangle

To find the missing ones, we need to sum the last two numbers in each composition.

C3(4)C_3(4) based on C3(3)C_3(3)Obtained by Sum
1,1,1,1\langle 1,1,1,1 \rangle1,1,2\langle 1,1,2 \rangle
2,1,1\langle 2,1,1 \rangle2,2\langle 2,2 \rangle
1,2,1\langle 1,2,1 \rangle1,3\langle 1,3 \rangle
3,1\langle 3,1 \rangle4\langle 4 \rangle

We can quickly see that there is an invalid composition: 4\langle 4 \rangle. If we remove all the invalid compositions we obtain remain with every 3-bounded compositions for N=4N=4: therefore, C3(4)=7|C_3(4)| = 7.

At this point we can develop a quick relation between C3(N)|C_3(N)| and C3(N1)|C_3(N-1)|: the number of 33-bounded compositions for NN are 2 times the number of compositions for N1N-1 minus the number of invalid compositions.

Not a big improvement eh? Don’t worry and follow me.

Now, we can note the fact that the number of invalid compositions depends on the number of compositions of N1N-1 ending with 33. It is in fact easy to see that the only way to get an invalid composition when we sum the last two numbers of a composition is when the corresponding C3(N1)C_3(N-1) composition ends with a 3. In fact, in that case, adding the extra 11 during the “sum” step will overflow.

Let’s call γx(N)\gamma_x(N) the number of composition of NN ending with xx and write the resulting formula:

C3(N)=2C3(N1)γ3(N1)|C_3(N)| = 2 |C_3(N-1)| - \gamma_3(N-1)

At this point, we can reiterate the same intuition and think about the number γ3(N1)\gamma_3(N-1). The number of compositions ending with 33 of N1N-1 depends on the number of compositions ending with 22 in N2N-2. We can then rewrite the above formula in this way:

C3(N)=2C3(N1)γ2(N2)|C_3(N)| = 2 |C_3(N-1)| - \gamma_2(N-2)

And then again:

C3(N)=2C3(N1)γ1(N3)|C_3(N)| = 2 |C_3(N-1)| - \gamma_1(N-3)

At this point we go back to the table and see that the only compositions ending with 11 are the one coming from the previous iteration. In fact, all the compositions after the “sum” step must be at least greater than 2.

γ1(N3)=C3(N4)\gamma_1(N-3) = |C_3(N-4)|

We are now ready to rewrite the entire formula in function of C3|C_3|:

C3(N)=2C3(N)C3(N4)|C_3(N)| = 2 |C_3(N)| - |C_3(N-4)|

We can verify this for C3(4)|C_3(4)|:

C3(4)=2C3(3)C3(0)=241=7|C_3(4)| = 2 |C_3(3)| - |C_3(0)| = 2 \cdot 4 - 1 = 7

Equivalence with the Tribonacci Formula

In that format is hard to see, but the formula we got is another way to express the Tribonacci formula.

In the following section we will show that

f(n)=2f(n1)f(n4)f(n) = 2*f(n-1) - f(n-4)

is equivalent to

f(n)=f(n1)+f(n2)+f(n3)f(n) = f(n-1) + f(n-2) + f(n-3)

In fact, we can rewrite

f(n1)=f(n2)+f(n3)+f(n4)f(n-1) = f(n-2) + f(n-3) + f(n-4)

And then subtract the second from the first:

f(n)f(n1)=f(n1)+f(n2)f(n2)+f(n3)f(n3)f(n4)f(n) - f(n-1) = f(n-1) + f(n-2) - f(n-2) + f(n-3) - f(n-3) - f(n-4)

The terms with n2n-2 and n3n-3 cancels out leaving us with:

f(n)f(n1)=f(n1)f(n4)f(n) - f(n-1) = f(n-1) - f(n-4)

And, finally, moving things around:

f(n)=2f(n1)f(n4)f(n) = 2*f(n-1) - f(n-4)

Corollary and Conclusions

Of course, there is nothing special about the number 3.

The same reasoning can be reiterated for any arbitrary maximum allowed integer. I already checked the 2-bounded compositions and, as predicted, they follow the classic Fibonacci sequence.

It is then easy to see that nn-bounded compositions will follow the nn-bonacci sequence. In fact, we can use the same same line of reasoning reaching the conclusion that the number of nn-bounded compositions fall in the following pattern

Cn(N)=2Cn(N)Cn(N(n+1))|C_n(N)| = 2 |C_n(N)| - |C_n(N-(n+1))|

And that any formula in the form:

f(n)=2f(n1)f(n(c+1))f(n) = 2f(n-1) - f(n-(c+1))

Is just an alternative way to write a generic cc-bonacci formula:

f(n)=1cf(nc)f(n) = \sum_1^c f(n-c)

This does not qualify as a rigorous proof, but I am happy with the result.

I am sorry if I feel excited for something that may be well known in the mathematical community. I just thought it was another surprising appearance of the Fibonacci sequence, and it was fun to share.