Skip to content

Instantly share code, notes, and snippets.

@cellularmitosis
Created August 3, 2018 05:53
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 cellularmitosis/567d7fb89b35e1e8cf7743ad5a278b4e to your computer and use it in GitHub Desktop.
Save cellularmitosis/567d7fb89b35e1e8cf7743ad5a278b4e to your computer and use it in GitHub Desktop.
(def table [
["M" 1000]
["D" 500]
["CD" 400]
["C" 100]
["L" 50]
["XL" 40]
["X" 10]
["IX" 9]
["V" 5]
["IV" 4]
["I" 1]])
(defn roman [i table]
(cond
(= i 0) ""
(= table '()) ""
:else
(let [
symbl (first (first table))
value (last (first table))
quotient (unchecked-divide-int i value)
roman-head-str (apply str (repeat quotient symbl))
roman-tail-str (roman (- i (* quotient value)) (rest table))]
(apply str roman-head-str roman-tail-str))))
(defn test-roman [i]
(printf "roman(%s): %s\n" i (roman i table)))
(run! test-roman [1 3 4 5 6 8 9 10 11 14 15 16 39 40 49 50 99 100])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment