Skip to content

Instantly share code, notes, and snippets.

@featheredtoast
Created October 8, 2015 20:18
Show Gist options
  • Save featheredtoast/a5e28266a23a1dfb86e9 to your computer and use it in GitHub Desktop.
Save featheredtoast/a5e28266a23a1dfb86e9 to your computer and use it in GitHub Desktop.
clojure happy number http://www.4clojure.com/problem/86
(defn happy-numbers
[n]
(letfn [(next-happy [n]
(->> (str n)
(partition 1 1)
(map first)
(take-while #(not (= \. %)))
(map (comp #(Math/pow % 2) read-string str))
(reduce +)))
(repeats? [coll]
(->> coll
frequencies
(filter #(< 1 (second %)))
((complement empty?))))]
(->> (iterate inc 1)
(map #(take % (iterate next-happy n)))
(drop-while #(not (or (== 1 (last %)) (repeats? %))))
first
(#(if (== 1 (last %)) true false)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment