Skip to content

Instantly share code, notes, and snippets.

@dfuenzalida
Last active January 27, 2020 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dfuenzalida/efcc554552be626bab08d22a39195de0 to your computer and use it in GitHub Desktop.
Save dfuenzalida/efcc554552be626bab08d22a39195de0 to your computer and use it in GitHub Desktop.
Solución Calculando Capicuas de Programando.org en Clojure
(ns capicuas.part2)
(defn reverso [res n]
(if (zero? n)
res
(recur (+' (* 10 res) (rem n 10)) (quot n 10))))
(defn not-palin? [n]
(not= n (reverso 0 n)))
(defn next-capicua [n]
(+' n (reverso 0 n)))
(defn iters-capicua [n]
(let [[its caps] (->> (iterate next-capicua n)
rest
(take 1000) ;; max numero de iteraciones
(split-with not-palin?))]
(if (first caps)
(inc (count its))
0)))
;; Tests:
;; (iters-capicua 57) ;; => 2
;; (iters-capicua 43) ;; => 1
;; (iters-capicua 77) ;; => 3
;; Solucion:
;; (apply max-key iters-capicua (range 100 1001)) ;; => 880
;; (iters-capicua 880) ;; => 23
;; (->> (iterate next-capicua 880) (drop-while not-palin?) first) ;; => 8813200023188
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment