Create a gist now

Instantly share code, notes, and snippets.

@j1n3l0 /gist:5239956
Last active Dec 15, 2015

(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 __
(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)
(let [a (first coll)
b (second coll)
f (if (and a b (< a b)) - +)]
(__ (rest coll) (f acc a))))))

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


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