Skip to content

Instantly share code, notes, and snippets.

@kgn
Created June 7, 2016 06:22
Show Gist options
  • Select an option

  • Save kgn/7715a8d2422fbf201fc49c628d1834d7 to your computer and use it in GitHub Desktop.

Select an option

Save kgn/7715a8d2422fbf201fc49c628d1834d7 to your computer and use it in GitHub Desktop.
func romanNumerals(input: Int) -> String {
var number = input
let numerals = ["M", "M", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
let values = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
var numeralString = ""
for (i, numeral) in numerals.enumerate() {
while number >= values[i] {
number -= values[i]
numeralString += numeral
}
}
return numeralString
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment