Advertisement
vlatkovski

subtract by adding

May 4th, 2015
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.78 KB | None | 0 0
  1.  
  2.  
  3. function subtract(num1, num2)
  4.     if 0 > num1 or 0 > num2 then return "can't subtract negative numbers sorry lol" end
  5.  
  6.     local res = ""
  7.  
  8.     local n1 = num1-num2 > 0 and num1 or num2
  9.     local n2 = tostring( n1 == num1 and num2 or num1 )
  10.  
  11.     for i=1,#n2-1 do
  12.         --print( n2:sub(i,i) )
  13.         res = res .. tostring( 9 - tonumber(n2:sub(i,i)) )
  14.     end
  15.     res = res .. 10 - n2:sub(#n2, #n2)
  16.  
  17.     return tonumber( tostring( n1 + tonumber(res) ):sub(2, -1) ) -- where you actually add the numbers you get
  18. end
  19.  
  20.  
  21.  
  22. -------------------------
  23.  
  24. for i = 0, 100 do
  25.     local x, y = math.random(1000000), math.random(1000000)
  26.  
  27.     if y>x then x,y = y,x end
  28.  
  29.     print(("-"):rep(10))
  30.     print(string.format("%s - %s", x, y))
  31.     print( "=> " .. subtract(x, y) )
  32. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement