Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function string.remove(s, str)
- s = tostring(s)
- local a, b = s:match("(.-)" .. str .. "(.+)")
- return (a and b) and a .. b or s, s:find(str) and #s-s:find(str) or 0
- end
- function sum(a, b)
- local ret = ""
- a, na = string.remove(a, "%-")
- b, nb = string.remove(b, "%-")
- a = ("0"):rep(#b-#a) .. a
- b = ("0"):rep(#a-#b) .. b
- if (na == 0 and nb == 0) or (na ~= 0 and nb ~= 0) then
- local reminder = 0
- for i = #a, 1, -1 do
- local v = tonumber(a:sub(i, i))+tonumber(b:sub(i, i))+reminder
- if v < 10 then
- reminder = 0
- ret = v .. ret
- else
- reminder = tonumber(tostring(v):sub(1, 1))
- ret = tostring(v):sub(#tostring(v)) .. ret
- end
- end
- if reminder ~= 0 then
- ret = reminder .. ret
- end
- end
- if na ~= 0 and nb ~= 0 then
- ret = "-" .. ret
- end
- return ret
- end
- function dif(a, b)
- local ret = ""
- a, na = string.remove(a, "%-")
- b, nb = string.remove(b, "%-")
- a = ("0"):rep(#b-#a) .. a
- b = ("0"):rep(#a-#b) .. b
- if bigger(b, a) then
- local ba = a
- a = b
- b = ba
- end
- local reminder = 0
- for i = #a, 1, -1 do
- local v = tonumber(a:sub(i, i))-tonumber(b:sub(i, i))-reminder
- if v < 0 then
- v = v+10
- reminder = 1
- ret = v .. ret
- else
- reminder = 0
- ret = v .. ret
- end
- end
- return ret
- end
- function prod(a, b)
- local ret = ""
- local values = {}
- local reminder = 0
- a, ca = string.remove(a, "%.")
- b, cb = string.remove(b, "%.")
- a, na = string.remove(a, "%-")
- b, nb = string.remove(b, "%-")
- for ia = #a, 1, -1 do
- for ib = #b, 1, -1 do
- local v = tonumber(a:sub(ia, ia))*tonumber(b:sub(ib, ib))+reminder
- if v < 10 then
- reminder = 0
- ret = v .. ret
- else
- reminder = tonumber(tostring(v):sub(1, 1))
- ret = tostring(v):sub(#tostring(v)) .. ret
- end
- end
- if reminder ~= 0 then
- ret = reminder .. ret
- end
- table.insert(values, ret)
- ret = ""
- reminder = 0
- end
- local total = "0"
- for i = 1, #values do
- total = sum(total, values[i] .. ("0"):rep(i-1))
- end
- if ca+cb > 0 then
- total = total:sub(1, #total-ca-cb) .. "." .. total:sub(#total-ca-cb+1)
- end
- if (na ~= 0 and nb == 0) or (nb ~= 0 and na == 0) then
- total = "-" .. total
- end
- return total
- end
- function bigger(a, b)
- a = ("0"):rep(#b-#a) .. a
- b = ("0"):rep(#a-#b) .. b
- for i = 1, #a do
- if tonumber(a:sub(i, i)) > tonumber(b:sub(i, i)) then
- return true
- elseif tonumber(a:sub(i, i)) < tonumber(b:sub(i, i)) then
- return false
- end
- end
- end
- function table.reverse(tab)
- local ret = {}
- for i = #tab, 1, -1 do
- table.insert(ret, tab[i])
- end
- return ret
- end
- function mathfloor(str)
- return str:match("(.+)%.") or str
- end
- function sqroot(n, amount)
- amount = amount+1
- local npairs = {}
- local root = ""
- n = tostring(n)
- for i = 1, #n, 2 do
- table.insert(npairs, n:sub(#n-i, #n-i+1))
- end
- npairs = table.reverse(npairs)
- root = root .. mathfloor(tostring(math.sqrt(tonumber(npairs[1]))))
- local rest = tostring(tonumber(npairs[1])-math.floor(math.sqrt(tonumber(npairs[1])))^2)
- local nextvalue = 2
- local comma
- while rest and bigger(rest, "0") and #root < amount do
- if npairs[nextvalue] then
- rest = rest .. npairs[nextvalue]
- else
- rest = rest .. "00"
- if not comma then
- comma = nextvalue
- end
- end
- droot = prod(root, 2) .. "0"
- while bigger(rest, prod(droot, droot:sub(#droot))) and bigger("9", droot:sub(#droot)) do
- droot = droot:sub(1, #droot-1) .. sum(droot:sub(#droot), "1")
- end
- if bigger(prod(droot, droot:sub(#droot)), rest) then
- droot = droot:sub(1, #droot-1) .. dif(droot:sub(#droot), "1")
- end
- root = root .. droot:sub(#droot)
- rest = dif(rest, prod(droot, droot:sub(#droot)))
- nextvalue = nextvalue+1
- end
- if comma then
- return root:sub(1, comma-1) .. "." .. root:sub(comma)
- else
- return root
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement