Robonado

Equação Quadrática

Apr 30th, 2015
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.47 KB | None | 0 0
  1. y = 'x²-4x+4'; -- f(x)
  2.  
  3. a,b,c = y:match('(%p*%d*)%a+²(%p%d+)%a*(%p*%d*)');
  4. a,b,c = a == '-' and -1 or tonumber(a),tonumber(b),tonumber(c);
  5. a,b,c = not a and 1 or a, not c and not y:sub(-1):find('%a') and 0 or b, c or y:sub(-1):find('%a') and 0 or y:match('²(%p%d+)');
  6. r = {(-b +math.sqrt(b^2 -4*a*c)) / (2*a), (-b -math.sqrt(b^2 -4*a*c)) / (2*a)};
  7.  
  8. print(table.sort(r) or y:match('%a')..' = {'..(r[1] ~= r[1] and '' or r[1] ~= r[2] and table.concat(r, ', ') or r[1])..'}')
Advertisement
Add Comment
Please, Sign In to add comment