Skip to content

Instantly share code, notes, and snippets.

@lmarinho
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lmarinho/f642f3773d83f72f1f48 to your computer and use it in GitHub Desktop.
Save lmarinho/f642f3773d83f72f1f48 to your computer and use it in GitHub Desktop.
(defn digits [n]
(->> n
(iterate #(quot % 10))
(take-while #(> % 0))
(map #(rem % 10))
reverse))
(defn happy-step [n] (->> n digits (map #(* % %)) (reduce +)))
(defn happy-set [n]
(loop
[ steps-so-far #{}
current-step n]
(if (contains? steps-so-far current-step)
steps-so-far
(recur
(conj steps-so-far current-step)
(happy-step current-step)))))
(defn is-happy? [n] (contains? (happy-set n) 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment