Skip to content

Instantly share code, notes, and snippets.

@malloc47 malloc47/sequence.clj
Last active Jun 28, 2016

Embed
What would you like to do?
(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
You can’t perform that action at this time.