Created
December 15, 2011 08:07
project euler 74
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
;; Problem 74 : 2011/12/14 | |
;; "Elapsed time: 135291.687352 msecs" | |
(defn factorial [n] | |
(reduce * (range 1 (inc n)))) | |
(defn-memo sum-of-factorial [num-list] | |
(reduce + (map factorial num-list))) | |
(defn exists [tgt col] | |
(some #(= tgt %) col)) | |
(defn get-loop [key col] | |
(let [loops (drop-while #(not= key %) col)] | |
(if (empty? loops) | |
false | |
[loops | |
(take-while #(not= key %) col)]))) | |
(let [mem (atom {})] | |
(defn mem-set-count-loop! [loop-list] | |
(let [loop-cnt (count loop-list)] | |
(dorun (map #(swap! mem assoc % loop-cnt) loop-list)))) | |
(defn mem-set-count-stem! [stem-list base] | |
(dorun (map #(swap! mem assoc %1 (+ base %2)) | |
(reverse stem-list) (iterate inc 1)))) | |
(defn count-74-link [num prec-vec] | |
(if-let [cnt (get @mem num)] | |
(do (mem-set-count-stem! prec-vec cnt) | |
(+ cnt (count prec-vec))) | |
(let [next-num (sum-of-factorial (sort (num-to-list num))) | |
new-vec (conj prec-vec num)] | |
(if-let [[looping stem] (get-loop next-num new-vec)] | |
(do (mem-set-count-loop! looping) | |
(mem-set-count-stem! stem (count looping)) | |
(count new-vec)) | |
(recur next-num new-vec))))) | |
) | |
(defn pe-74 [lmt] | |
(count | |
(filter #(= 60 %) | |
(map #(do (count-74-link % [])) (range 1 (inc lmt)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment