Created
October 8, 2015 20:18
-
-
Save featheredtoast/a5e28266a23a1dfb86e9 to your computer and use it in GitHub Desktop.
clojure happy number http://www.4clojure.com/problem/86
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-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