fork download
  1. (def my-solution2
  2.  
  3. (fn [start]
  4. (let [str-start (str start)
  5. start-a? (-> str-start count odd?)
  6. ;_ (println :start-a? start-a?)
  7. start-stage-pow (dec (Math/ceil (/ (count str-start) 2)))
  8. ;_ (println :start-stage-pow start-stage-pow)
  9. start-stage (Math/pow 10 start-stage-pow)
  10. ;_ (println :start-stage start-stage)
  11. str-start-left (clojure.string/join (take (inc start-stage-pow) str-start))
  12. ;_ (println :str-start-left str-start-left)
  13. start-stage-end (* 10 start-stage)
  14. ;_ (println :start-stage-end start-stage-end)
  15. start-left (inc (Long/parseLong str-start-left))
  16. ;_ (println :start-left start-left)
  17. start-palindrome (if start-a?
  18. (clojure.string/join (cons str-start-left (reverse (butlast str-start-left))))
  19. (clojure.string/join (cons str-start-left (reverse str-start-left))))
  20. ;_ (println :start-palindrome start-palindrome)
  21. ]
  22.  
  23. (filter #(>= % start)
  24. (map #(Long/parseLong (first %))
  25. (iterate
  26. (fn [[palindrome, a? stage stage-end left]]
  27.  
  28. (let [stage-end? (= (inc left) stage-end)
  29.  
  30. new-a? (if stage-end? (not a?) a?)
  31. new-stage (if (and
  32. (not a?)
  33. stage-end?) (* 10 stage) stage)
  34. new-left (if stage-end? new-stage (inc left))
  35.  
  36. str-left (str left)
  37. new-palindrome (if a?
  38. (clojure.string/join (cons str-left (reverse (butlast str-left))))
  39. (clojure.string/join (cons str-left (reverse str-left))))
  40.  
  41. ret-val [new-palindrome
  42. new-a?
  43. new-stage
  44. (* 10 new-stage)
  45. new-left]
  46. ]
  47. ret-val
  48.  
  49. ))
  50. ; initial values
  51. [start-palindrome
  52. start-a?
  53. (int start-stage) (int start-stage-end),
  54. start-left]
  55. )))))
  56.  
  57. )
  58.  
  59. (time (println (take 1 (my-solution2 (* 111111111 111111111)))))
Success #stdin #stdout 1.58s 389120KB
stdin
Standard input is empty
stdout
(12345678987654321)
"Elapsed time: 22.487728 msecs"