Lecture 42 (Tychonievich) - Exam Review

Lecture Date: Monday, April 27

See Final Exam Review Guide

Created during questions about recursion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class Exam3Review {
  public static int fib(int i) {
      if (i < 1) {
          return 1;
      } else {
          int x = fib(i - 1);
          int y = fib(i - 2);
          return x + y;
      }
  }

  public static void main(String[] args) {
      System.out.println(fib(6));
      /*
      * fib(6) = fib(5) + fib(4) = 13 + 8 = 21
      * fib(5) = fib(4) + fib(3) = 8 + 6 = 13
      * fib(4) = fib(3) + fib(2) = 5 + 3 = 8
      * fib(3) = fib(2) + fib(1) = 3 + 2 = 5
      * fib(2) = fib(1) + fib(0) = 2 + 1 = 3
      * fib(1) = fib(0) + fib(-1) = 1 + 1 = 2
      * fib(0) = 1
      * fib(-1) = 1
      */
  }
}

Notes created during Q&A