Solving equations recursively

100 Views Asked by At
 I am looking at the solution of the following question and wondering how is f(7) equal to 247  in line 1 ?

Given a recursive formula in (1), find the value of f(7) 
f(n) =  (2f(n − 1) + n if n > 0
         and 0 if n = 0

1.f(7) = 2f(6) + 7 = 247
2.f(6) = 2f(5) + 6 = 120
3.f(5) = 2f(4) + 5 = 57
4.f(4) = 2f(3) + 4 = 26
5.f(3) = 2f(2) + 3 = 11
6.f(2) = 2f(1) + 2 = 4
7.f(1) = 2f(0) + 1 = 1
8.f(0) = 0
1

There are 1 best solutions below

0
On BEST ANSWER

I'm going to spell out what I think Mufasa means in their comment. First you write:

$$f(7) = 2\;f(6)+7$$

But you don't know what $f(6)$ is. So you try to compute that:

$$f(6) = 2\;f(5) + 6$$

But you don't know what $f(5)$ is either. So you want to compute that, which requires you to work out $f(4)$, which requires $f(3)$, which requires $f(2)$, which requires $f(1)$. But that last one we can do:

$$f(1) = 2\;f(0) + 1 = 2(0) + 1 = 1$$

Now we can backchain:

$$\begin{eqnarray*} f(1) &=& 1\\ f(2) &=& 2\;f(1)+2 = 2(1)+2 = 4\\ &\vdots&\\ f(7) &=& 2\;f(6)+7 = 2(120)+7 = 247 \end{eqnarray*}$$