Skip to content

Instantly share code, notes, and snippets.

@j1n3l0
Last active December 15, 2015 09:39
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 j1n3l0/5239956 to your computer and use it in GitHub Desktop.
Save j1n3l0/5239956 to your computer and use it in GitHub Desktop.
(fn [n]
(when (and (< n 4000) (> n 0))
(loop [n n
m {1000 "M"
900 "CM"
500 "D"
400 "CD"
100 "C"
90 "XC"
50 "L"
40 "XL"
10 "X"
9 "IX"
5 "V"
4 "IV"
1 "I"}
[f & d] (reverse (sort (keys m)))
r []]
(if (zero? n)
(apply str (flatten r))
(recur (mod n f) m d (conj r (repeat (unchecked-divide-int n f) (m f))))))))
(fn __
([coll]
(let [coll (map {\I 1 \V 5 \X 10 \L 50 \C 100 \D 500 \M 1000} coll)]
(__ coll 0)))
([coll acc]
(if-not (seq coll)
acc
(let [a (first coll)
b (second coll)
f (if (and a b (< a b)) - +)]
(__ (rest coll) (f acc a))))))
@j1n3l0
Copy link
Author

j1n3l0 commented Mar 25, 2013

After several iterations, a version of Integer to Roman Numerals I'm somewhat happy with :)

@j1n3l0
Copy link
Author

j1n3l0 commented Nov 30, 2013

And this is the reverse: Roman to Arabic converter :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment