Created
December 26, 2013 03:44
-
-
Save ChanglinZhou/8129550 to your computer and use it in GitHub Desktop.
Palindromic Numbers
This file contains hidden or 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
(let [exp10 #(reduce * 1 (repeat % 10)) | |
combine (fn [n1 ev] | |
(loop [r n1 n2 (if ev n1 (quot n1 10))] | |
(if (zero? n2) r | |
(recur (+ (* 10 r) (rem n2 10)) (quot n2 10))))) | |
gen (fn gen [start end ev] | |
(lazy-cat | |
(if ev nil (map #(combine % ev) (range start end))) | |
(map #(combine % true) (range (if ev start (quot end 10)) end)) | |
(gen end (* end 10) false))) | |
source (lazy-cat [0] (gen 1 10 false)) | |
] | |
(fn [n] | |
(if (< n 10) (drop-while #(< % n) source) | |
(let [s (str n) | |
ev (even? (.length s)) | |
hl (- (.length s) (int (/ (.length s) 2))) | |
h (read-string (.substring s 0 hl)) ] | |
(drop-while #(< % n) (gen h (exp10 hl) ev)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment