Advertisement
Guest User

Untitled

a guest
May 2nd, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.79 KB | None | 0 0
  1. function string.remove(s, str)
  2.     s = tostring(s)
  3.     local a, b = s:match("(.-)" .. str .. "(.+)")
  4.     return (a and b) and a .. b or s, s:find(str) and #s-s:find(str) or 0
  5. end
  6.  
  7. function sum(a, b)
  8.     local ret = ""
  9.     a, na = string.remove(a, "%-")
  10.     b, nb = string.remove(b, "%-")
  11.     a = ("0"):rep(#b-#a) .. a
  12.     b = ("0"):rep(#a-#b) .. b
  13.     if (na == 0 and nb == 0) or (na ~= 0 and nb ~= 0) then
  14.         local reminder = 0
  15.         for i = #a, 1, -1 do
  16.             local v = tonumber(a:sub(i, i))+tonumber(b:sub(i, i))+reminder
  17.             if v < 10 then
  18.                 reminder = 0
  19.                 ret = v .. ret
  20.             else
  21.                 reminder = tonumber(tostring(v):sub(1, 1))
  22.                 ret = tostring(v):sub(#tostring(v)) .. ret
  23.             end
  24.         end
  25.         if reminder ~= 0 then
  26.             ret = reminder .. ret
  27.         end
  28.     end
  29.     if na ~= 0 and nb ~= 0 then
  30.         ret = "-" .. ret
  31.     end
  32.     return ret
  33. end
  34.  
  35. function dif(a, b)
  36.     local ret = ""
  37.     a, na = string.remove(a, "%-")
  38.     b, nb = string.remove(b, "%-")
  39.     a = ("0"):rep(#b-#a) .. a
  40.     b = ("0"):rep(#a-#b) .. b
  41.     if bigger(b, a) then
  42.         local ba = a
  43.         a = b
  44.         b = ba
  45.     end
  46.     local reminder = 0
  47.     for i = #a, 1, -1 do
  48.         local v = tonumber(a:sub(i, i))-tonumber(b:sub(i, i))-reminder
  49.         if v < 0 then
  50.             v = v+10
  51.             reminder = 1
  52.             ret = v .. ret
  53.         else
  54.             reminder = 0
  55.             ret = v .. ret
  56.         end
  57.     end
  58.     return ret
  59. end
  60.  
  61. function prod(a, b)
  62.     local ret = ""
  63.     local values = {}
  64.     local reminder = 0
  65.     a, ca = string.remove(a, "%.")
  66.     b, cb = string.remove(b, "%.")
  67.     a, na = string.remove(a, "%-")
  68.     b, nb = string.remove(b, "%-")
  69.     for ia = #a, 1, -1 do
  70.         for ib = #b, 1, -1 do
  71.             local v = tonumber(a:sub(ia, ia))*tonumber(b:sub(ib, ib))+reminder
  72.             if v < 10 then
  73.                 reminder = 0
  74.                 ret = v .. ret
  75.             else
  76.                 reminder = tonumber(tostring(v):sub(1, 1))
  77.                 ret = tostring(v):sub(#tostring(v)) .. ret
  78.             end
  79.         end
  80.         if reminder ~= 0 then
  81.             ret = reminder .. ret
  82.         end
  83.         table.insert(values, ret)
  84.         ret = ""
  85.         reminder = 0
  86.     end
  87.     local total = "0"
  88.     for i = 1, #values do
  89.         total = sum(total, values[i] .. ("0"):rep(i-1))
  90.     end
  91.     if ca+cb > 0 then
  92.         total = total:sub(1, #total-ca-cb) .. "." .. total:sub(#total-ca-cb+1)
  93.     end
  94.     if (na ~= 0 and nb == 0) or (nb ~= 0 and na == 0) then
  95.         total = "-" .. total
  96.     end
  97.     return total
  98. end
  99.  
  100. function bigger(a, b)
  101.     a = ("0"):rep(#b-#a) .. a
  102.     b = ("0"):rep(#a-#b) .. b
  103.     for i = 1, #a do
  104.         if tonumber(a:sub(i, i)) > tonumber(b:sub(i, i)) then
  105.             return true
  106.         elseif tonumber(a:sub(i, i)) < tonumber(b:sub(i, i)) then
  107.             return false
  108.         end
  109.     end
  110. end
  111.  
  112. function table.reverse(tab)
  113.     local ret = {}
  114.     for i = #tab, 1, -1 do
  115.         table.insert(ret, tab[i])
  116.     end
  117.     return ret
  118. end
  119.  
  120. function mathfloor(str)
  121.     return str:match("(.+)%.") or str
  122. end
  123.  
  124. function sqroot(n, amount)
  125.     amount = amount+1
  126.     local npairs = {}
  127.     local root = ""
  128.     n = tostring(n)
  129.     for i = 1, #n, 2 do
  130.         table.insert(npairs, n:sub(#n-i, #n-i+1))
  131.     end
  132.     npairs = table.reverse(npairs)
  133.     root = root .. mathfloor(tostring(math.sqrt(tonumber(npairs[1]))))
  134.     local rest = tostring(tonumber(npairs[1])-math.floor(math.sqrt(tonumber(npairs[1])))^2)
  135.     local nextvalue = 2
  136.     local comma
  137.     while rest and bigger(rest, "0") and #root < amount  do
  138.         if npairs[nextvalue] then
  139.             rest = rest .. npairs[nextvalue]
  140.         else
  141.             rest = rest .. "00"
  142.             if not comma then
  143.                 comma = nextvalue
  144.             end
  145.         end
  146.         droot = prod(root, 2) .. "0"
  147.         while bigger(rest, prod(droot, droot:sub(#droot))) and bigger("9", droot:sub(#droot)) do
  148.             droot = droot:sub(1, #droot-1) .. sum(droot:sub(#droot), "1")
  149.         end
  150.         if bigger(prod(droot, droot:sub(#droot)), rest) then
  151.             droot = droot:sub(1, #droot-1) .. dif(droot:sub(#droot), "1")
  152.         end
  153.         root = root .. droot:sub(#droot)
  154.         rest = dif(rest, prod(droot, droot:sub(#droot)))
  155.         nextvalue = nextvalue+1
  156.     end
  157.     if comma then
  158.         return root:sub(1, comma-1) .. "." .. root:sub(comma)
  159.     else
  160.         return root
  161.     end
  162. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement