#150 / Palindromic Numbers

62 views
Skip to first unread message

aaron

unread,
Jun 23, 2017, 4:40:56 PM6/23/17
to 4Clojure
Below is my first pass to problem #150, which seems to cause 4clojure to hang. My solution runs quite fast on my local machine, is 4clojure down? Or suffering performance problems? Or is something about my solution wrong? Thanks for any help!


(fn [n]
(
letfn [(pal->num [pal]
(
reduce-kv #(+ %1 (* (int (Math/pow 10 %2)) %3)) 0 (vec pal)))
(
pals [w i]
(
cond
(= 1 w) (map list (range i 10))
(
= 2 w) (map #(list % %) (range i 10))
:else (
let [sub (pals (- w 2) 0)]
(
mapcat
(fn [n] (map #(concat (list n) % (list n)) sub))
(
range i 10)))))]
(
let [s (str n)
w (
count s)]
(
filter #(>= % n)
(
cons 0
(map pal->num (mapcat #(pals % 1) (iterate inc w))))))))

aaron

unread,
Jun 24, 2017, 9:23:29 PM6/24/17
to 4Clojure
For future reference, this prior answer solved this problem for me:

Reply all
Reply to author
Forward
0 new messages