Skip to content

Instantly share code, notes, and snippets.

@twopoint718
Created June 20, 2009 01:30
Show Gist options
  • Save twopoint718/132990 to your computer and use it in GitHub Desktop.
Save twopoint718/132990 to your computer and use it in GitHub Desktop.
multiplication table in Clojure
(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