Created
April 23, 2013 02:49
-
-
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)
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
;; 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