TITLE: How can I rewrite recursive function as single formula?
QUESTION [7 upvotes]: There is following recursive function
$$
\begin{equation}
    a_n=
    \begin{cases}
      -1, & \text{if}\ n = 0 \\
      1, & \text{if}\ n = 1\\
      10a_{n-1}-21a_{n-2}, & \text{if}\ n \geq 2
    \end{cases}
\end{equation}
$$
I know this can be rewritten as 
$$
a_n=7^n-2\cdot3^n
$$
But how can I reach that statement? I found this problem on some particular website. My skills are not enough to solve such things. Someone told me I have to read about Generating function but it didn't help me. 
I would be thankful if someone explained it to me.

REPLY [4 votes]: This is a homogeneous linear recurrence relation with constant coefficients. From
$$
a_n = 10 a_{n-1} -21 a_{n-2} 
$$
you can infer the order $d=2$ and the characteristic polynomial:
$$
p(t) = t^2 - 10 t + 21
$$
Calculating the roots:
$$
0 = p(t) = (t - 5)^2 - 25 + 21 \iff
t = 5 \pm 2
$$
this gives the general solution
$$
a_n = k_1 3^n + k_2 7^n
$$
The two constants have to be determined from two initial conditions:
$$
-1 = a_0 = k_1 + k_2 \\
1 = a_1 = 3 k_1 + 7 k_2
$$
This leads to $4 = 4 k_2$ or $k_2 = 1$ and thus $k_1 = -2$.
So we get
$$
a_n = -2 \cdot 3^n + 7^n
$$