I am trying to learn Dynamic Programming and one of the examples they give in Wikipedia of what is not Dynamic Programming, is a recursive way of getting Fibonacci sequence up to certain number. I.E.
Given a recursive function, say:
fib(n) = 0 if n = 0
1 if n = 1
fib(n - 1) + fib(n - 2) if n >= 2
We can easily write this recursively from its mathematic form as:
function fib(n)
if(n == 0 || n == 1)
n
else
fib(n-1) + fib(n-2)
But I cannot get the pseudo code to work.
when I do this method in Java, I get an error the operator + is undefined for methods void:
public void fib(int n) {
if (n == 0 || n == 1) {
System.out.println(n);
} else
return fib(n - 1) + fib(n - 2);
}