Facebook
From FrancisPostsHere, 4 Years ago, written in Plain Text.
This paste is a reply to Algebraic Quadratic Equation Solver from FrancisPostsHere - go back
Embed
--To use this, simply write the following at the very end: 'Solve(a,b,c)' [where a,b,c are Integers.]
--Example: Solve(1,-2,3)
--It solves the following equation: ax^2 + bx + c = 0.
--After writing, execute the code.
--It outputs the answers in the form of square roots and fractions, instead of decimals.
--It also gives other information about the equation made from the input.

function FP(numbers)
if #numbers>0 then
local F = {}
 for k=1, #numbers do
 local n = numbers[k]
 local n2 = math.abs(n)
 local f = {{1,1}}
 if n2 ~= 1 then
 while n2>1 do
  for i=2, math.abs(n) do
   if n2>1 then
    if math.floor(n2/i)==n2/i then
    local lol = 1
     while lol >= 1 do
     local LOL = i^(lol+1)
      if math.floor(n2/LOL)~=n2/LOL then
       f[#f+1] = {i,lol}
       n2 = n2/(i^lol)
       lol = 0
      else
       lol = lol+1
      end
     end
    end
   end       
  end
 end
 end
 F[#F+1]=f
 end
return F
end
end
function FCF(pon)
 local af = FP({pon[1]})
 local bf = FP({pon[2]})
 local cf = {{1,1}}
 for i=1, #af[1] do
  for k=1, #bf[1] do
   local f1 = af[1][i]
   local f2 = bf[1][k]
   local s = nil
   if f1[1]==f2[1] then
    if f1[2] >= f2[2] then
     s=f2[2]
    elseif f1[2] < f2[2] then
     s=f1[2]
    end
    cf[#cf+1]={f1[1],math.abs(s)}
   end
  end
 end
 return cf
end
function ECF(pon2)
 local f = FCF(pon2)
 local cf=1
 for i=1, #f do
 cf = cf*(f[i][1]^f[i][2])
 end
 return cf
end
function Solve(a,b,c)
local result = nil
if tonumber(a)~=nil and tonumber(b)~=nil and tonumber(c)~=nil then
if a == math.floor(a) and b == math.floor(b) and c == math.floor(c) then
if a ~= 0 then
local t = 1
local s = {}
local S = 1
local D = (b*b)-(4*a*c)
 if D<0 then
 t=-1
 end
 if math.abs(D)>0 then
  local Df = FP({D})
  for k1=1, #Df[1] do
   local f = Df[1][k1]
   if f[2] == 1 then
    t=t*f[1]
   elseif f[2]/2 ~= math.floor(f[2]/2) and f[2]>1 then
    f[2]=f[2]-1
    s[#s+1]=f[1]^f[2]
    t=t*f[1]
   elseif f[2]/2 == math.floor(f[2]/2) then
    s[#s+1]=f[1]^f[2]
   end
  end
  for k2=1, #s do
  S = S*math.sqrt(s[k2])
  end
  if t~=1 then
   local ncf = ECF({b,S})
   local fcf = ECF({2*a,ncf})
   n1 = ((-1*b)/fcf)
   n2 = (S/fcf)
   d = (2*a/fcf)
   if n1 == 0 then
   local fcf4 = ECF({2*a,S})
   n2 = S/fcf4
   d = 2*a/fcf4
   end
  end
 elseif D == 0 then
 t=0
 end
 if t == 0 then
  local fcf1 = ECF({b,2*a})
  local n = b/fcf1
  local da = 2*a/fcf1
   if math.abs(da) ~= 1 and math.abs(da) ~= math.abs(n) then
    result = "SOLUTION: "..tostring(-1*n).."/"..tostring(da).." | FACTORABLE | 1 REAL SOLUTION"
   else
    result = "SOLUTION: "..tostring(-1*n/da).." | FACTORABLE | 1 REAL SOLUTION"
   end
  elseif t>1 then
  if n2 == 1 then
  n2 = ""
  end
  if n1 ~= 0 then
  result = "SOLUTIONS: ("..tostring(n1).." + "..tostring(n2).."sqrt("..tostring(t).."))/"..tostring(d).." and ("..tostring(n1).." - "..tostring(n2).."sqrt("..tostring(t).."))/"..tostring(d).." | NOT FACTORABLE | 2 REAL SOLUTIONS"
  elseif n1 == 0 then
  result = "SOLUTIONS: "..tostring(n2).."sqrt("..tostring(t)..")/"..tostring(d).." and (-"..tostring(n2).."sqrt("..tostring(t).."))/"..tostring(d).." | NOT FACTORABLE | 2 REAL SOLUTIONS"
  end
  elseif t<0 then
  if n2 == 1 then
   n2 = ""
  end
  t=math.abs(t)
  if n1 ~= 0 then
   if t ~= 1 then
   result = "SOLUTIONS: ("..tostring(n1).." + "..tostring(n2).."sqrt("..tostring(t)..")i)/"..tostring(d).." and ("..tostring(n1).." - "..tostring(n2).."sqrt("..tostring(t)..")i)/"..tostring(d).." | NO REAL SOLUTIONS"
   elseif t ==1 then
   result = "SOLUTIONS: ("..tostring(n1).." + "..tostring(n2).."i)/"..tostring(d).." and ("..tostring(n1).." - "..tostring(n2).."i)/"..tostring(d).." | NO REAL SOLUTIONS"
   end
  elseif n1 == 0 then
   if t ~= 1 then
   result = "SOLUTIONS: "..tostring(n2).."sqrt("..tostring(t)..")i)/"..tostring(d).." and (-"..tostring(n2).."sqrt("..tostring(t)..")i)/"..tostring(d).." | NO REAL SOLUTIONS"
   elseif t ==1 then
   result = "SOLUTIONS: "..tostring(n2).."i/"..tostring(d).." and (-"..tostring(n2).."i)/"..tostring(d).." | NO REAL SOLUTIONS"
   end
  end
 elseif t==1 then
  local fcf2 = ECF({2*a,(-b)+S})
  local fcf3 = ECF({2*a,b+S})
  local n1a = (((-b)+S)/fcf2)
  local n2a = ((b+S)/fcf3)
  local db1 = (2*a/fcf2)
  local db2 = (2*a/fcf3)
   if math.abs(db) ~= 1 then
    result = "SOLUTIONS: "..tostring(n1a).."/"..tostring(db1).." and ".."("..tostring(-1*n2a)..")/"..tostring(db2).." | FACTORABLE | 2 REAL SOLUTIONS"
   else
    result = "SOLUTIONS: "..tostring(n1a/db1).." and "..tostring(-1*n2a/db2).." | FACTORABLE | 2 REAL SOLUTIONS"
   end
 end
 elseif a == 0 and b ~= 0 then
 local lcf = ECF({b,c})
 local ln = -c/lcf
 local ld = b/lcf
 if math.abs(ld) ~= 1 and math.abs(ld) ~= math.abs(c) and ln ~= 0 then
 result = "SOLUTION: "..tostring(ln).."/"..tostring(ld).." | 
This is a linear equation."
 else
 result = "SOLUTION: "..tostring(ln/ld).." | This is a linear equation."
 end
 elseif a==0 and b == 0 then
 result = "NO SOLUTIONS | This is a constant..."
 if c == 0 then
 result = "SOLUTION: ANY NUMBER | What equation is this?"
 end
 end
else
  result = "This program only works on integers :("
end
else
 result = "Please check your input, it must be in 
Lua Code by the form of Solve(INTEGER,INTEGER,INTEGER)"
end
print(result)
end
--Example--------------
Solve(2,3,5)
------------Remove this
way..