Skip to content

Instantly share code, notes, and snippets.

@malloc47
Last active June 28, 2016 14:34
Show Gist options
  • Save malloc47/a73daa45ef0bfd27ec1c6b4d933b8992 to your computer and use it in GitHub Desktop.
Save malloc47/a73daa45ef0bfd27ec1c6b4d933b8992 to your computer and use it in GitHub Desktop.
(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