I need to solve this recurrence function: f(n) = 5*f(n-1) – 2*f(n-2), with f(0)=1 and f(1)=2. I wrote the below code, but it is not giving the correct answer — it outputs 164, for example, when n = 4, although the correct answer is 26 (assuming I did my math correctly).
public static int recurFunction(int n) {
if(n == 0) {
return 1;
} else if(n == 1) {
return 2;
} else {
n = ((5 * recurFunction(n - 1)) - (2 * recurFunction(n - 2)));
return n;
}
}
Your math is wrong 🙂
f(n) = 5*f(n-1) – 2*f(n-2)