Solución Calculando Capicuas de Programando.org en Clojure
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
(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