Created
October 8, 2013 19:25
-
-
Save anonymous/6890101 to your computer and use it in GitHub Desktop.
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 palindrome [n] | |
(let [ to-digits (fn [k] (into [] (map #(mod (quot k %) 10) | |
(take-while #(< 0 (quot k %)) (iterate #(* 10 %) 1) )))) | |
to-number (fn [k] (apply + (map * k (iterate #(* 10 %) 1)))) | |
left-digits #(subvec % (/ (count % ) 2)) | |
even-digits? #(even? (count %)) | |
mirror (fn [[num dig]] | |
(let [ digits (to-digits num)] | |
(if (= :even dig) (concat (reverse digits) digits) | |
(concat (reverse (drop 1 digits)) digits)))) | |
init #(let [ld (left-digits %)] | |
(vector (to-number ld) | |
(if (even-digits? %) :even :odd) | |
(long (Math/pow 10 (count ld))))) | |
nextp (fn [[num even goal]] | |
(let [m (inc num)] | |
(if (= m goal) | |
(if (= even :even) | |
[goal :odd (* 10 goal)] | |
[(/ goal 10) :even goal]) | |
[m even goal] ))) | |
i (init (to-digits n)) | |
palindromes (iterate nextp i) ] | |
(filter (partial <= n ) (map (comp to-number mirror) palindromes)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment