Created
December 14, 2010 22:33
-
-
Save will/741240 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
;; 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 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
;; 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)) |
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
(defn split [n] | |
(cond | |
(< n 10) [n] | |
:else (conj | |
(split (quot n 10)) | |
(rem n 10)))) | |
(defn next-chain [n] | |
(reduce + (map #(* % %) (split n)))) | |
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
(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