Last active
December 16, 2015 04:58
-
-
Save pbalduino/5380528 to your computer and use it in GitHub Desktop.
Another example of memoization with Clojure, also using Fibonacci
This file contains hidden or 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
; without memoization | |
(defn fib [n] | |
(if (or (zero? n) (= n 1)) | |
1 | |
(+ (fib (dec n) ) (fib (- n 2))))) | |
(time (fib 90)) | |
; spent more than 30 minutes and I quit | |
; I don't know if it sounds right, but worked fine | |
(def fib-memo) | |
(defn fib [n] | |
(if (or (zero? n) (= n 1)) | |
1 | |
(+ (fib-memo (dec n) ) (fib-memo (- n 2))))) | |
(def fib-memo (memoize fib)) | |
(time (fib 90)) | |
; "Elapsed time: 0.058 msecs" | |
; 4660046610375530309 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment