Skip to content

Instantly share code, notes, and snippets.

@pbalduino
Last active December 16, 2015 04:58
Show Gist options
  • Save pbalduino/5380528 to your computer and use it in GitHub Desktop.
Save pbalduino/5380528 to your computer and use it in GitHub Desktop.
Another example of memoization with Clojure, also using Fibonacci
; 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