Created
June 20, 2009 01:30
-
-
Save twopoint718/132990 to your computer and use it in GitHub Desktop.
multiplication table in Clojure
This file contains hidden or 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
(defn multtab [n] | |
(for [row (range n) col (range n)] | |
(* (inc row) (inc col)))) | |
;; reasonably nicely formatted | |
(defn main [n] | |
(let [width (int (Math/ceil (/ (Math/log (* n n)) (Math/log 10)))) | |
digit-str (str "%" (inc width) "d")] | |
(doseq [row (partition n (multtab n))] | |
(let [formatted-row (map #(format digit-str %) row)] | |
(println (apply str formatted-row)))))) | |
;; if you don't care about formatting | |
(defn show-multtab [n] | |
(doseq [r (partition n (multtab n))] | |
(println (apply str (interpose " " r))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment