Skip to content

Instantly share code, notes, and snippets.

@SixArm
Created March 3, 2015 22:23
Show Gist options
  • Save SixArm/a83405d36fb4406ca0db to your computer and use it in GitHub Desktop.
Save SixArm/a83405d36fb4406ca0db to your computer and use it in GitHub Desktop.
Roman numeral conversion sample code
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