Created
May 30, 2020 11:09
-
-
Save mamapitufo/e2404a1bc09b060163d0481327d817e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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