Advertisement
Bolodefchoco_LUAXML

[Math] Int & Roman

Apr 20th, 2016
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.12 KB | None | 0 0
  1. --Creator: Bolodefchoco
  2. --Made in: 20/04/2016
  3. --Last update: 26/05/2016
  4. --[[ Notes:
  5.     math.intToRoman
  6.         Does:
  7.             Caso int seja maior que 0 e menor que 4000, será transformado em números romanos
  8.         Args:
  9.             int --> Número > 0,< 4000
  10.     math.romanToInt
  11.         Does:
  12.             Retorna o número romano introduzido em número.
  13.         Args:
  14.             str --> Número romano (String)
  15. ]]--
  16.  
  17. math.intToRoman=function(int)
  18.     if int<1 or int>3999 then error("Int must be a number between 1 and 3999") end
  19.     local romans = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}
  20.     local arabics = {1000,900,500,400,100,90,50,40,10,9,5,4,1}
  21.     local result = ""
  22.     for i = 1,#arabics do
  23.         local count = math.floor(tonumber(int/arabics[i]))
  24.         result = result .. romans[i]:rep(count)
  25.         int = int - (arabics[i] * count)
  26.     end
  27.     return result
  28. end
  29.  
  30. math.romanToInt=function(str)
  31.     str = str:upper()
  32.     local nums = {M=1000,D=500,C=100,L=50,X=10,V=5,I=1}
  33.     local int = 0
  34.     for i = 1,#str do
  35.         local value = nums[str:sub(i,i)]
  36.         if i+1 < #str and nums[str:sub(i+1,i+1)] > value then
  37.             int = int - value
  38.         else
  39.             int = int + value
  40.         end
  41.     end
  42.     return int
  43. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement