CS2
Recursion
10
Be careful
n
public int fibonacci(int n) {
n
if (n == 0 || n == 1) {
n
return 1;
n
}
n
else {
n
return fibonacci(n-1)+fibonacci(n-2);
n
}
n
}
n
•
This looks fine, but in fact is disastrously
n
slow because it unnecessarily repeats some
n
calculations over and over again!