Skip to content

Instantly share code, notes, and snippets.

@mamapitufo
Created May 30, 2020 11:09
Show Gist options
  • Save mamapitufo/e2404a1bc09b060163d0481327d817e8 to your computer and use it in GitHub Desktop.
Save mamapitufo/e2404a1bc09b060163d0481327d817e8 to your computer and use it in GitHub Desktop.
(ns fibo)
(defn fibo-rec [n]
(cond
(= n 0) 0
(= n 1) 1
:else (+ (fibo-rec (- n 1))
(fibo-rec (- n 2)))))
(defn fibo-tail-rec [n]
(loop [n n
[a b] [0 1]]
(cond
(= n 0) a
(= n 1) b
:else (recur (dec n) [b (+ a b)]))))
(println "(fibo-rec 40) = " (time (fibo-rec 40)))
; (out) "Elapsed time: 2614.9296 msecs"
; (out) (fibo-rec 40) = 102334155
(println "(fibo-tail-rec 40) = " (time (fibo-tail-rec 40)))
; (out) "Elapsed time: 0.057141 msecs"
; (out) (fibo-tail-rec 40) = 102334155
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment