Skip to content

Instantly share code, notes, and snippets.

@uvtc
Created April 23, 2013 02:49
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 uvtc/5440450 to your computer and use it in GitHub Desktop.
Save uvtc/5440450 to your computer and use it in GitHub Desktop.
find and count number palindromes (convert the int to a string, then see if it's the same backwards)
;; try1.clj
(require '[clojure.string :as str])
(defn count-palindromes
[max]
(let [n (atom 0)]
(doseq [i (range 10 max)]
(let [i-str (str i)
i-rstr (str/reverse i-str)]
(if (= i-str i-rstr)
(swap! n inc))))
@n))
(println "total num of them:" (count-palindromes 1e7))
;;--------------------------------------------------------
;; try2.clj
(require '[clojure.string :as str])
(defn count-palindromes
[max]
(count (filter (fn [s]
(let [i-str (str s)
i-rstr (str/reverse i-str)]
(= i-str i-rstr)))
(range 10 max))))
(println "total num of them:" (count-palindromes 1e7))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment