An elementary way of solving recurrences

January 7, 2024 · 12 min · 2510 words · nor

Introduction

A lot of people shy away from solving (mathematical) recurrences just because the theory is not very clear/approachable due to not having an advanced background in math.

As a consequence, the usual ways of solving recurrences tend to be:

  • Find the first few terms on OEIS.
  • Guess terms from the rate of growth of the recurrence (exponential rate of growth means you can sometimes estimate the exponential terms going from largest to smallest --- though this fails in cases where there is a double-root of the characteristic equation)
  • Use some theorem whose validity you can't prove (the so-called characteristic equation method)
  • Overkill using generating functions

But this doesn't have to be the case, because there is a nice method you can apply to solve equations reliably. I independently came up with this method back when I was in middle school, and surprisingly a lot of people have no idea that you can solve recurrences like this.

The method is more principled and reliable than the first two guesswork methods, the third method might fail to apply in some cases, and the fourth method requires knowing what an infinite series is. An added bonus is that it prevents you from forgetting special cases that often plague recurrences! Meanwhile, as we show in the last section, having intuition about the method also allows you to apply the ideas developed to things that are only vaguely related to recurrences.

The best thing about the method is that it requires nothing more than school-level algebra and potentially some intuition/common sense.

I'm not saying that you should only use this method, just that keeping it in mind is pretty helpful whenever you're faced with a recurrence (or even more fundamentally, linear combinations of numbers).

A simple example

Note: I recommend working through this section (with pen and paper) with the examples xk=2xk1+3x_k = 2x_{k-1} + 3 and xk=xk1+5x_k = x_{k-1} + 5. Using symbols can be intimidating for some and thus examples should be used while presenting new ideas, but it is also important to understand the intuition behind the underlying ideas in the form of algebra.

Let's first consider a recurrence xk=axk1+bx_k = ax_{k-1} + b. We employ wishful thinking: what is there was no bb in the recurrence? In that case, we would have had xk=x0akx_k = x_0 \cdot a^k, and that would be the end of the story. So can we reduce the general case to one where b=0b = 0?

If you define yk=xk+cy_k = x_k + c (i.e., vertically shift the graph of (i,xi)(i, x_i) by some offset), then we have yk=a(yk1c)+b+cy_k = a(y_{k-1} - c) + b + c. Now we have one degree of freedom, and we can choose cc such that the constant term ac+b+c-ac + b + c is 00, i.e., c=ba1c = \frac{b}{a-1}. Note that we immediately run into a special case here: a=1a = 1 --- in this case, such a cc is impossible to choose. So we make two cases to finish off the problem:

  • a=1a = 1: In this case, we have xk=xk1+bx_k = x_{k-1} + b. This means that xx is an arithmetic progression and not a geometric progression, and xk=x0+bkx_k = x_0 + bk.
  • a1a \ne 1: In this case, there is a cc such that yk=ayk1y_k = ay_{k-1}, so yk=aky0y_k = a^k y_0. Translating it back into xx-space (expressing yy in terms of xx), we have xk+c=ak(x0+c)x_k + c = a^k (x_0 + c) which gives us xk=akx0+ak1a1bx_k = a^k x_0 + \frac{a^k - 1}{a - 1}b.

Exercise: Try to solve the recurrence xk=2xk12x_k = 2 x_{k-1}^2 using this method.

A slightly harder example

Note: I recommend working through this section with two separate examples in mind: xk=3xk1+10xk2x_k = 3x_{k-1} + 10x_{k-2} and xk=4xk14xk2x_k = 4x_{k-1} - 4x_{k-2}.

Let's now consider the recurrence xk=axk1+bxk2+cx_k = a x_{k-1} + b x_{k-2} + c. For the sake of simplicity of presentation, we note that we can do something like the previous example to get rid of the cc, so let's assume c=0c = 0. So we want to solve xk=axk1+bxk2x_k = a x_{k-1} + b x_{k-2}.

Now how do we deal with the two lagging xk1x_{k-1} and xk2x_{k-2} terms instead of just one? The trick is to try and find dd such that yk=xkdxk1y_k = x_k - d \cdot x_{k-1} becomes a geometric progression (we can also use a +d+d instead of d-d like we did in the previous solution, but that does not matter).

We can also look at it in this way: we want to subtract a suitable multiple of xk1x_{k-1} from both sides, so that xkdxk1x_k - d x_{k-1} (for some rr) becomes a multiple of its lagged sequence.

For that to happen, notice that we must have yk=eyk1y_k = e y_{k-1}. This translates to xkdxk1=e(xk1dxk2)x_k - d x_{k-1} = e (x_{k-1} - d x_{k-2}). Comparing it with the original recurrence, we must have d+e=ad + e = a and de=bde = - b. So we have |de|=(d+e)24de=a2+4b|d - e| = \sqrt{(d + e)^2 - 4de} = \sqrt{a^2 + 4b} (note how this relates to solving a quadratic equation: x2axbx^2 - ax - b needs to be factorized into (xd)(xe)(x - d)(x - e) --- this will be helpful later on). Using this, we can solve for dd and ee.

Now once that we have dd and ee, we have yk=eyk1y_k = e y_{k-1}, so yk=ek1y1y_k = e^{k-1} y_1. Let's now, as earlier, go back to xx-space. We have xkdxk1=ek1y1x_k - d x_{k-1} = e^{k-1} y_1: note that y1=(x1dx0)y_1 = (x_1 - d x_0) is a constant we already know.

We now have two methods of solving this: one is to simply add these equalities for different kk in a telescoping manner, after dividing the whole equation by dkd^k. Just for the sake of showing the usage of this method a bit more, I will go with the other one.

Looking at the new recurrence we got for xx, we again want to define some zkz_k as a function of xkx_k, such that zk=dzk1z_k = d z_{k-1}. Since there a term proportional to ek1e^{k-1} on the right, we will choose zk=xk+fek1z_k = x_k + f \cdot e^{k-1}.

Going back to the recurrence in xx, we get zkfek1dzk1+dfek2=ek1y1z_k - f \cdot e^{k-1} - d \cdot z_{k-1} + d \cdot f \cdot e^{k-2} = e^{k-1} \cdot y_1.

To get rid of the non-zz terms, we must have f(de)=ey1f \cdot (d - e) = ey_1. We are again faced with two cases, as in the previous example.

The case with ded \ne e (the "distinct-root" case) is easy: in this case, we can just solve for the problem analogously to what we did to solve the simpler example.

The only remaining case is d=ed = e (the "repeated-root" case): let's look closely at why we failed here. The two terms coming from xkx_k and from xk1x_{k-1} cancel out completely. What can we do to be able to have different contributions of ek1e^{k-1} from both of these terms? Let's choose zk=xk+g(k)ek1z_k = x_k + g(k) \cdot e^{k-1} instead. Then we must have g(k1)g(k)=y1g(k - 1) - g(k) = y_1. This means g(k)g(k) is in fact a linear function of gg. That is, we have g(k)=hk+ig(k) = hk + i for some h,ih, i: here h=y1h = -y_1 and ii can be set to anything arbitrary (for now let's say i=0i = 0). So we choose g(k)=y1kg(k) = -y_1 k, i.e., zk=xky1kek1z_k = x_k - y_1 \cdot k \cdot e^{k-1}.

Since we know zk=dzk1z_k = dz_{k-1}, we know that zk=dkz0z_k = d^k z_0. Going into xx-space (expressing zz in terms of xx), we have a solution to the equation.

A short sketch of how to deal with higher order recurrences

In this section, just by generalizing the approach in the previous section, we will show how you can in fact derive the method of characteristic equations while solving recurrences.

Remember the note about solving for dd and ee. It looks like finding a factorization of x2axbx^2 - ax - b in the form (xd)(xe)(x - d)(x - e).

When you try to use the method above for third order recurrences, you will quickly realize that we are trying to find a factorization of x3ax2bxcx^3 - ax^2 - bx - c into (xd)(x2ex+f)(x - d)(x^2 - ex + f) for the first step, and as you go along solving the lower order recurrence at every step, you will realize that you also need to factorize x2ex+fx^2 - ex + f into two linear polynomials.

If the roots rir_i (i=1i = 1 to 33) of x3ax2bxcx^3 - ax^2 - bx - c are all distinct, you will get xkx_k in the nice form xk=i=13cirikx_k = \sum_{i=1}^3 c_i r_i^k. But if the roots have multiplicity more than 1 (i.e., (xr)s(x - r)^s divides the polynomial for some s>1s > 1), then the cic_i becomes a polynomial in kk with degree s1s - 1.

Using induction, you can show that a result of this form is indeed true for higher order recurrences as well.

An example slightly unrelated to recurrences

There's a funny story related to this method. Back when I was testing [contest:1909], problem D was not in the problemset. When it was added (with k=1k = 1 as the easier version, the problem was supposed to be problem B in the set), I solved the problem instantly because I used this method for solving recurrences before, and thought that its difficulty is supposed to be not more than A. A lot of people disagreed with me on this, and it was eventually agreed upon to use general kk as a harder problem --- I still disagreed with this opinion for a while, but then I realized that not a lot of people know this method of approaching recurrences/linear combinations.

This was partly the motivation behind writing this post (the other part was the existence of posts like this).

Here's how you solve that problem using this method.

In this problem, you are given a reverse recurrence of the form y+z=x+ky + z = x + k. Inspired by the method developed above, we will just subtract kk from both sides, and let x=xk,y=yk,z=zkx' = x - k, y' = y - k, z' = z - k, to get y+z=xy' + z' = x'.

This means that if we shift all the array elements vertically by k-k, we are just splitting elements into other elements 1k\ge 1 - k.

(Arguably) the hardest part of the problem is to realize that you can shift numbers and deal with them as usual, after the transformation. But to a person who knows this method well, it might seem like this problem was made specifically to be solved by it.

Conclusion

In this post, we saw that we can solve recurrences of the form ak=f(ak1,,akl)a_k = f(a_{k-1}, \dots, a_{k - l}) by trying to impose easy-to-analyze structure on expressions of the form bk=g(ak,ak1,,akl+1)b_k = g(a_{k}, a_{k-1}, \dots, a_{k - l + 1}). In each example, the structure was of the form bk=rbk1b_k = r b_{k-1}, which gives us a way to compute bkb_k directly, thereby reducing the "order" of the problem from l+1l + 1 to ll. If you tried the exercise, the structure was supposed to be of the form bk=bk12b_k = b_{k-1}^2.

In a way, this is an application of the divide and conquer way of thinking (in the general sense). This kind of divide and conquer, but in a bottom-up way rather than a top-down way, is also used when you derive things like Hensel's lifting lemma.

Cite this post
@online{recurrence-trick-2024,
  author    = {nor},
  title     = {An elementary way of solving recurrences},
  year      = {2024},
  month     = {01},
  day       = {07},
  url       = {https://nor-blog.pages.dev/posts/2024-01-07-recurrence-trick},
}