Created
July 9, 2011 04:28
-
-
Save davidedicillo/1073310 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def translate_to_roman num | |
| roman = '' | |
| roman = roman + 'M' * (num / 1000) | |
| if (num % 1000) >= 900 | |
| roman = roman + 'CM' | |
| else | |
| roman = roman + 'D' * (num % 1000 / 500) | |
| end | |
| if (num % 500) >= 400 && (num % 1000) < 900 | |
| roman = roman + 'CD' | |
| elsif (num % 1000) < 900 | |
| roman = roman + 'C' * (num % 500 / 100) | |
| end | |
| if (num % 100) >= 90 | |
| roman = roman + 'XC' | |
| elsif (num % 100) < 90 && (num % 100) >= 50 | |
| roman = roman + 'L' * (num % 100 / 50) + 'X' * (num % 50 / 10) | |
| elsif (num % 100) < 50 && (num % 100) >= 40 | |
| roman = roman + 'XL' | |
| else | |
| roman = roman + 'X' * (num % 50 / 10) | |
| end | |
| if (num % 10) >= 9 | |
| roman = roman + 'IX' | |
| elsif (num % 10) < 9 && (num % 10) >= 5 | |
| roman = roman + 'V' * (num % 10 / 5) + 'X' * (num % 5) | |
| elsif (num % 10) < 5 && (num % 10) >= 4 | |
| roman = roman + 'IV' | |
| else | |
| roman = roman + 'I' * (num % 5) | |
| end | |
| end | |
| puts 'Tell me a number' | |
| x = gets.chomp.to_i | |
| puts translate_to_roman(x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment