mips - f(n), understanding the equation -


i've been tasked writing mips instruction code following formula:

f(n) = 3 f(n-1) + 2 f(n-2)   f(0) = 1 f(1) = 1 

i'm having issues understanding formula means.

from understand passing int n doubly recursive program.

so f(0) equation be:

f(n)=3*1(n-1) + 2*(n-2) 

if n=10 equation be:

f(10)=3*1(10-1) + 2*(10-2) 

i know i'm not getting right @ because wouldn't recursive. light shed on equation means great. should able write mips code once understand equation.

i think it's difference equation.

you're given 2 starting values:

f(0) = 1 f(1) = 1 f(n) = 3*f(n-1) + 2*f(n-2) 

so can keep going this:

f(2) = 3*f(1) + 2*f(0) = 3 + 2 = 5 f(3) = 3*f(2) + 2*f(1) = 15 + 2 = 17 

so recursive method (i'll write java-like notation):

public int f(n) {     if (n == 0) {         return 1;     } else if (n == 1) {         return 1;      } else {          return 3*f(n-1) + 2*f(n-2); // see? recursion happens here.     } } 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -