public

  • Download Gist
ummels-4clojure-solution150.clj
Clojure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
;; ummels's solution to Palindromic Numbers
;; https://4clojure.com/problem/150
 
(fn [n]
(letfn [(decode [n] (if (< n 10) [n] (conj (decode (quot n 10)) (rem n 10))))
(encode [x] (reduce #(+ (* % 10) %2) 0 x))
(next-pal [n]
(let [N (decode n)
l (count N)
d (quot l 2)
H (take d N)
H1 (decode (inc (encode H)))
Hr (reverse H)
h (encode Hr)
p (nth N d)
t (encode (take-last d N))]
(encode (cond
(and (even? l) (>= h t)) (concat H Hr)
(and (odd? l) (>= h t)) (concat H [p] Hr)
(even? l) (concat H1 (reverse H1))
(and (odd? l) (< p 9)) (concat H [(inc p)] Hr)
:else (concat H1 [0] (reverse H1))))))]
(iterate (comp next-pal inc) (next-pal n))))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.