Last active
June 28, 2016 14:34
-
-
Save malloc47/a73daa45ef0bfd27ec1c6b4d933b8992 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
(defn happy-next | |
[n] | |
(let [digits (->> n str seq (map #(Character/digit % 10)))] | |
(->> (map * digits digits) | |
(apply +)))) | |
(defn happy' | |
[n seen] | |
(loop [n n seen seen] | |
(cond | |
(= 1 n) true | |
(contains? seen n) false | |
:else (recur (happy-next n) (conj seen n))))) | |
(defn happy? | |
[n] | |
(happy' n #{})) | |
(defn happy* | |
[n] | |
(cons n (lazy-seq (happy* (happy-next n))))) | |
(defn happy? | |
[n] | |
(->> (happy* n) | |
(reductions conj []) | |
(take-while #(= (distinct %) %)) | |
(last) | |
(last) | |
(= 1))) | |
(def happy-seq | |
(->> (range) | |
(map inc) | |
(filter happy?))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment