Created
March 3, 2015 22:23
-
-
Save SixArm/a83405d36fb4406ca0db to your computer and use it in GitHub Desktop.
Roman numeral conversion sample code
This file contains 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
class Roman | |
Memo = {} | |
Code = [["","I","II","III","IV","V","VI","VII","VIII","IX"], | |
["","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"], | |
["","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"], | |
["","M","MM","MMM"]] | |
def self.to_roman(n) | |
Memo[n] ||= begin | |
l = Math.log10(n).to_i | |
q,r = n.divmod(10**l) | |
Code[l][q] + (r > 0 ? to_roman(r) : "") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment