Advertisement
JasonGronn

QuadForm

May 23rd, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.28 KB | None | 0 0
  1. print("Quadratic formula solver\nBoth the Lua and TI-Basic editions of this were written by EveryOS")
  2. print("X=(-B+/-_/(B^2-4A(C-D)))/2*a\nAX^2+BX+C=D")
  3. write("a: ")
  4. local a = tonumber(read())
  5. write("b: ")
  6. local b = tonumber(read())
  7. write("c: ")
  8. local c = tonumber(read())
  9. write("d: ")
  10. local d = tonumber(read())
  11. c=c-d
  12. term.clear()
  13. term.setCursorPos(1,1)
  14. print("Please wait...")
  15.  
  16. local ipart = math.floor
  17. local function getGCF(n1, n2)
  18.   local f = 1
  19.   for i = 1, n1 do
  20.     if (n1/i == ipart(n1/i)) and (n2/i == ipart(n2/i)) then
  21.       f = i
  22.     end
  23.   end
  24.   return f
  25. end
  26.  
  27. local function tofrac(a, b)
  28.   if a == 0 then return "0" end
  29.   if (b/getGCF(a, b))==1 then return tostring(a/getGCF(a, b)) end
  30.   return tostring(a/getGCF(a, b)).."/"..tostring(b/getGCF(a,b))
  31. end
  32.  
  33. d=b^2-4*a*c
  34. local e = -b/(2*a)
  35. if math.sqrt(d) == ipart(math.sqrt(d)) then
  36.   print("X="..tofrac(math.sqrt(d)-b, 2*a).."\n -or-\nX="..tofrac(math.sqrt(d)+b, 2*a))
  37. else
  38.   local f = 1
  39.   for i=1, d do
  40.     if ipart(d/(i^2)) == d/(i^2) then
  41.       f = i
  42.     end
  43.   end
  44.   local str = tofrac(-b, 2*a).."+/-(" .. (((getGCF(f, 2*a)~=1) and getGCF(f, 2*a)) or "")..((d<0 and "i") or "").."_/"..math.abs(d)/(f^2)..")"
  45.   if 2*a/getGCF(f,2*a) then
  46.     str = str.."/"..tostring(2*a/getGCF(f,2*a))
  47.   end
  48.   print(str)
  49. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement