Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print("Quadratic formula solver\nBoth the Lua and TI-Basic editions of this were written by EveryOS")
- print("X=(-B+/-_/(B^2-4A(C-D)))/2*a\nAX^2+BX+C=D")
- write("a: ")
- local a = tonumber(read())
- write("b: ")
- local b = tonumber(read())
- write("c: ")
- local c = tonumber(read())
- write("d: ")
- local d = tonumber(read())
- c=c-d
- term.clear()
- term.setCursorPos(1,1)
- print("Please wait...")
- local ipart = math.floor
- local function getGCF(n1, n2)
- local f = 1
- for i = 1, n1 do
- if (n1/i == ipart(n1/i)) and (n2/i == ipart(n2/i)) then
- f = i
- end
- end
- return f
- end
- local function tofrac(a, b)
- if a == 0 then return "0" end
- if (b/getGCF(a, b))==1 then return tostring(a/getGCF(a, b)) end
- return tostring(a/getGCF(a, b)).."/"..tostring(b/getGCF(a,b))
- end
- d=b^2-4*a*c
- local e = -b/(2*a)
- if math.sqrt(d) == ipart(math.sqrt(d)) then
- print("X="..tofrac(math.sqrt(d)-b, 2*a).."\n -or-\nX="..tofrac(math.sqrt(d)+b, 2*a))
- else
- local f = 1
- for i=1, d do
- if ipart(d/(i^2)) == d/(i^2) then
- f = i
- end
- end
- 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)..")"
- if 2*a/getGCF(f,2*a) then
- str = str.."/"..tostring(2*a/getGCF(f,2*a))
- end
- print(str)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement