Fibonacci Functions

Let's look at two versions of this function:


(defun fib2 (n)           public int fib2(int n) {
  (if (< n 2)                if (n < 2)
      n                         return n;
                                else return
      (+ (fib2 (- n 2))            fib2(n - 2) +
         (fib2 (- n 1)) ) ) )      fib2(n - 1);  }


(defun fib1 (n) (fib1b 0 1 n))

(defun fib1b (lo hi steps)
  (if (<= steps 0)
      lo
      (fib1b hi (+ lo hi) (- steps 1)) ) )


public int fib1(int n) { return fib1b(0, 1, n); }

public int fib1b(int lo, int hi, int steps) {
  if (steps <= 0)
     return lo;
     else return fib1b(hi, (lo + hi), steps - 1); }

Contents    Prev    Next    Page+10    Index