You start at `a' and then you have three choices at each step. Of the possible paths, only 1/4 of them will end at the right spot, so the formula is pow(3,n)/4. Well, powers of 3 are not usually divisible by 4, so you need to do some rounding. </mostly kidding>

Once you have the recursive formula, you can find the explicit formula by trying to find the numbers x such that pow(x,n) satisfies the recursive formula.

pow(x,n) = 3*pow(x,n-2) + 2*pow(x,n-1)

Dividing by pow(x,n-2), you get

x^2 = 3 + 2*x => x^2 - 2*x - 3 = 0

Solving the quadratic equation, you find out that 3 and -1 are the roots. Now all the sequences that satisfy the recursive formula form a vector space of dimension 2 (because the one that starts (1,0,...) and the one that starts (0,1,...) are a basis), and you just found two independent vectors. If you don't know what on Earth I am talking about, don't worry. It means that any sequence that satisfies the recursive formula can be expressed as a*pow(3,n) + b*pow(-1,n), for appropriate values of a and b. Since we know A(0)=0 and A(1)=1,

a*pow(3,0) + b*pow(-1,0) = 0 => a + b = 0

a*pow(3,1) + b*pow(-1,1) = 1 => 3*a - b = 1

Solving that system of linear equations, you get a=1/4, b=-1/4, and that gives us the explicit formula.

This may look like magic if you haven't seen it before, but it's just a procedure you learn.

**Edited by Álvaro, 31 October 2013 - 06:12 AM.**