int compareTo(T value);
private static void printInt(int toPrint) {
if (toPrint < 10) { /* first (or only) digit */
System.out.print(toPrint + " ");
} else {
/* print the digits before this one */
printInt(toPrint / 10);
/* print last digit -- could call printInt again recursively */
System.out.print(toPrint % 10 + " ");
}
}
static int fib(int n) {
if (n <= 2) {
return 1;
}
return fib(n - 1) + fib(n - 2);
}
public static int fib(n) {
if (n < 2) {
return 1;
}
fibHelper(1, 1, n - 2);
}
private static int fibHelper(first, second, n) {
if (n == 0) { // base case, end of recursion
return first + second;
}
return fibHelper(second, first + second, n - 1);
}
public static int fib(n) {
int first = 1;
int second = 1;
while (n >= 2) {
int third = first + second;
first = second;
second = third;
n--;
}
return second;
}