Skip to content

Commit

Permalink
Solved 92.
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenaustin committed Oct 6, 2011
1 parent c22985b commit 1e70a0a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
1 change: 0 additions & 1 deletion README.md
Expand Up @@ -45,7 +45,6 @@ in many cases the Clojure solution is very different from how I solved
it in Ruby. Here is my migration TODO list. Problems I have solved
in Ruby, but not yet in Clojure:

* 92
* 96
* 97
* 98
Expand Down
31 changes: 31 additions & 0 deletions src/dma/euler/p092.clj
@@ -0,0 +1,31 @@
(ns dma.euler.p092
(:use dma.euler.numeric))

;; Cleaner, but slow way to get the next number in the chain
;;
;; (defn sum-square-digits [n]
;; (sum (map square (digits n))))

(def squares (vec (map square (range 10))))

(defn sum-square-digits [n]
(loop [n n sum 0]
(if (zero? n)
sum
(recur (quot n 10) (+ sum (squares (rem n 10)))))))

(let [chain-endings (atom {1 1 89 89})]
(defn chain-end [n]
(loop [n n chain []]
(if-let [end (@chain-endings n)]
(do
(if (seq chain)
(swap! chain-endings #(apply assoc % (interleave chain (repeat end)))))
end)
(recur (sum-square-digits n) (conj chain n))))))

(defn solution {:answer 8581146} []
;; Note that we pass the number through sum-square-digits first
;; to get it down to a number less than 567 (for 9999999). That
;; keeps the cache manageable
(count (filter #(= 89 (chain-end (sum-square-digits %))) (range 1 10000000))))

0 comments on commit 1e70a0a

Please sign in to comment.