Skip to content

Instantly share code, notes, and snippets.

@kumo
Last active February 21, 2022 21:07
Show Gist options
  • Select an option

  • Save kumo/a8e1cb1f4b7cff1548c7 to your computer and use it in GitHub Desktop.

Select an option

Save kumo/a8e1cb1f4b7cff1548c7 to your computer and use it in GitHub Desktop.

Revisions

  1. kumo revised this gist Jul 7, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion RomanNumerals.swift
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,7 @@ func toRoman(number: Int) -> String {

    if (div > 0)
    {
    for j in 0..div
    for j in 0..<div
    {
    //println("Should add \(romanChar) to string")
    romanValue += romanChar
  2. kumo renamed this gist Jun 18, 2014. 1 changed file with 3 additions and 7 deletions.
    10 changes: 3 additions & 7 deletions RomanNumerals → RomanNumerals.swift
    Original file line number Diff line number Diff line change
    @@ -2,8 +2,6 @@

    import Foundation

    var arabicNumber = 2105

    func toRoman(number: Int) -> String {

    let romanValues = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
    @@ -23,15 +21,13 @@ func toRoman(number: Int) -> String {
    {
    //println("Should add \(romanChar) to string")
    romanValue += romanChar

    startingValue -= arabicValue
    }

    startingValue -= arabicValue * div
    }
    }

    return romanValue
    }

    toRoman(2105)

    toRoman(301)
    toRoman(2014)
  3. kumo created this gist Jun 18, 2014.
    37 changes: 37 additions & 0 deletions RomanNumerals
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    // Playground - noun: a place where people can play

    import Foundation

    var arabicNumber = 2105

    func toRoman(number: Int) -> String {

    let romanValues = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
    let arabicValues = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]

    var romanValue = ""
    var startingValue = number

    for (index, romanChar) in enumerate(romanValues) {
    var arabicValue = arabicValues[index]

    var div = startingValue / arabicValue

    if (div > 0)
    {
    for j in 0..div
    {
    //println("Should add \(romanChar) to string")
    romanValue += romanChar

    startingValue -= arabicValue
    }
    }
    }

    return romanValue
    }

    toRoman(2105)

    toRoman(301)