Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Creator: Bolodefchoco
- --Made in: 18/09/2016
- --Last update: 18/09/2016
- --[[ Notes:
- Does:
- Retorna o bhaskara da equação
- Args:
- e --> A equação (xy²+xy+x)
- ]]--
- local cfact = function(x)
- local num,den = x:match("(%d+)/(%d+)")
- num,den = tonumber(num),tonumber(den)
- while den ~= 0 do
- local t = den
- den = num%den
- num = t
- end
- return num
- end
- local isInteger = function(x)
- return math.floor(x) - x == 0
- end
- local simplify = function(x)
- local num,den = x:match("(%d+)/(%d+)")
- local f = cfact(x)
- local n,d = num/f,den/f
- if isInteger(n/d) then
- return n/d
- else
- return n.."/"..d
- end
- end
- math.reduceDec = function(x,n)
- local d = n and "1"..("0"):rep(n) or 100
- return math.floor(x*d)/d
- end
- math.toFraction = function(x,reduce)
- if reduce then
- x = math.reduceDec(x,1)
- end
- local int,dec = math.modf(x)
- if dec == 0 then
- return x
- else
- local numb,frac = tostring(x):match("(%d+)%.(%d+)")
- local fracDen = "1" .. ("0"):rep(#frac)
- local result = numb..frac.."/"..fracDen
- return simplify(result)
- end
- end
- local getDiv = function(d)
- if type(d) == "string" then
- local f,d = d:match("(%d+)/(%d+)")
- return f/d
- else
- return d
- end
- end
- math.bhaskara = function(e)
- e = e:gsub(" ","")
- local a,x,b,c = e:match("(%p*%d+)(%a*)²*.*(%p%d+).*(%p%d+)")
- a,b,c,x = tonumber(a),tonumber(b),tonumber(c),x or "x"
- local delta = (b^2) - 4*a*c
- delta = delta^.5
- if delta < 0 or delta ~= delta then
- return "Delta is negative"
- else
- local x1 = (-b + delta) / (2*a)
- local x2 = (-b - delta) / (2*a)
- x1,x2 = x1~=x1 and 0 or math.toFraction(x1,true),x2~=x2 and 0 or math.toFraction(x2,true)
- local max = getDiv(x1) > getDiv(x2) and {x1,x2} or {x2,x1}
- return x:upper() .. " = {"..max[1]..", "..max[2].."}"
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment