Claymer Documentation

1 Fibonacci Sequence Function

NoteOUT
sut/fib
[#object[sun.nio.fs.UnixPath 0x21b40343 /Users/lukezeitlin/claymer/example] #object[sun.nio.fs.UnixPath 0x1494fec5 /Users/lukezeitlin/claymer/example/example/core.clj]]
{:path #object[sun.nio.fs.UnixPath 0x6d432ce0 example/core.clj], :line 3}
[#object[sun.nio.fs.UnixPath 0x78dbe2fa /Users/lukezeitlin/claymer/example] #object[sun.nio.fs.UnixPath 0x7605aa82 /Users/lukezeitlin/claymer/example/example/core.clj]]
example.core/fib

1.0.1 fib

[n]

Takes a non-negative integer n and returns the nth number in the Fibonacci sequence.

1.1 Rich comments from example.core

(

;; this is an example comment
  (fib 10)
  ;; recursive
  (fib (fib 10))
  ;; functional
  (map fib (range 10))

)

1.2 Examples:

Produces nth number in the fibonacci sequence:

(deftest fib-test
  (testing
   (is (= (sut/fib 0) 0)))
  (testing
   (is (= (sut/fib 12) 144)))
  (testing
   (is (= (sut/fib 69) 117669030460994))))
PASS: (= 0 0)PASS: (= 144 144)PASS: (= 117669030460994 117669030460994)

Negitive numbers are out of bounds.

(deftest fib-neg-n-out-of-range-test
  (is
   (try (sut/fib -1)
        (catch Exception e
          (= (type e) java.lang.IndexOutOfBoundsException)))))
PASS: true

1.3 Properties

1.3.1 Parity Invarient

Every third Fibonacci number is even. This is because an odd + even = odd so we get the cycle:

0+1=1 -> 1+1=2 -> 1+2=3 -> ...
E+O=O -> O+O=E -> O+E=O -> ...
(def parity-invariant
  (prop/for-all
   [v gen/pos-int]
   (let [fib-number (sut/fib v)]
     (if (zero? (rem v 3))
       (even? fib-number)
       (odd? fib-number)))))
(tc/quick-check 10000 parity-invariant)
{:result true,
 :pass? true,
 :num-tests 10000,
 :time-elapsed-ms 60,
 :seed 1772375518663}

1.4 Performance Comparison

Below we compare the efficient iterative fib against a naive recursive fib-naive implementation across a range of input sizes. The naive version has exponential time complexity, so its execution time grows dramatically.

source: example/test/example/core_test.clj