Skip to content

Instantly share code, notes, and snippets.

@will
Created December 14, 2010 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save will/741240 to your computer and use it in GitHub Desktop.
Save will/741240 to your computer and use it in GitHub Desktop.
;; this one correctly memoizes all the recursive calls
;(defn next-chain [n] a-new-number)
(def chain-ends nil)
(defn get-ans [n] (chain-ends n))
(def get-ans (memoize get-ans))
(defn chain-ends [n]
(cond
(= n 1) 1
(= n 89) 89
:else (get-ans (next-chain n))))
;; this one doesn't. only the calls I make to chian-ends gets memoized
;; the internal recursive calls don't seem to get added to the memo.
(defn chain-ends [n]
(cond
(= n 1) 1
(= n 89) 89
:else (chain-ends n)))
(def chain-ends (memoize chain-ends))
(defn split [n]
(cond
(< n 10) [n]
:else (conj
(split (quot n 10))
(rem n 10))))
(defn next-chain [n]
(reduce + (map #(* % %) (split n))))
(defn split [n]
(cond
(< n 10) [n]
:else (conj
(split (quot n 10))
(rem n 10))))
(defn next-chain [n]
(reduce + (map #(* % %) (split n))))
(def chain-ends nil)
(defn get-ans [n] (chain-ends n))
(def get-ans (memoize get-ans))
(defn chain-ends [n]
(cond
(= n 1) 1
(= n 89) 89
:else (get-ans (next-chain n))))
(prn (time
(count
(filter
#(= % 89)
(map chain-ends (range 1 10000000))))))
;; "Elapsed time: 20488.703 msecs"
;; 8581146
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment