Facebook
From FrancisPostsHere, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 188
  1. --To use this, simply write the following at the very end: 'Solve(a,b,c)' [where a,b,c are Integers.]
  2. --Example: Solve(1,-2,3)
  3. --It solves the following equation: ax^2 + bx + c = 0.
  4. --After writing, execute the code.
  5. --It outputs the answers in the form of square roots and fractions, instead of decimals.
  6. --It also gives other information about the equation made from the input.
  7.  
  8. function FP(numbers)
  9. if #numbers>0 then
  10. local F = {}
  11.  for k=1, #numbers do
  12.  local n = numbers[k]
  13.  local n2 = math.abs(n)
  14.  local f = {{1,1}}
  15.  if n2 ~= 1 then
  16.  while n2>1 do
  17.   for i=2, math.abs(n) do
  18.    if n2>1 then
  19.     if math.floor(n2/i)==n2/i then
  20.     local lol = 1
  21.      while lol >= 1 do
  22.      local LOL = i^(lol+1)
  23.       if math.floor(n2/LOL)~=n2/LOL then
  24.        f[#f+1] = {i,lol}
  25.        n2 = n2/(i^lol)
  26.        lol = 0
  27.       else
  28.        lol = lol+1
  29.       end
  30.      end
  31.     end
  32.    end      
  33.   end
  34.  end
  35.  end
  36.  F[#F+1]=f
  37.  end
  38. return F
  39. end
  40. end
  41. function FCF(pon)
  42.  local af = FP({pon[1]})
  43.  local bf = FP({pon[2]})
  44.  local cf = {{1,1}}
  45.  for i=1, #af[1] do
  46.   for k=1, #bf[1] do
  47.    local f1 = af[1][i]
  48.    local f2 = bf[1][k]
  49.    local s = nil
  50.    if f1[1]==f2[1] then
  51.     if f1[2] >= f2[2] then
  52.      s=f2[2]
  53.     elseif f1[2] < f2[2] then
  54.      s=f1[2]
  55.     end
  56.     cf[#cf+1]={f1[1],math.abs(s)}
  57.    end
  58.   end
  59.  end
  60.  return cf
  61. end
  62. function ECF(pon2)
  63.  local f = FCF(pon2)
  64.  local cf=1
  65.  for i=1, #f do
  66.  cf = cf*(f[i][1]^f[i][2])
  67.  end
  68.  return cf
  69. end
  70. function Solve(a,b,c)
  71. local result = nil
  72. if tonumber(a)~=nil and tonumber(b)~=nil and tonumber(c)~=nil then
  73. if a == math.floor(a) and b == math.floor(b) and c == math.floor(c) then
  74. if a ~= 0 then
  75. local t = 1
  76. local s = {}
  77. local S = 1
  78. local D = (b*b)-(4*a*c)
  79.  if D<0 then
  80.  t=-1
  81.  end
  82.  if math.abs(D)>0 then
  83.   local Df = FP({D})
  84.   for k1=1, #Df[1] do
  85.    local f = Df[1][k1]
  86.    if f[2] == 1 then
  87.     t=t*f[1]
  88.    elseif f[2]/2 ~= math.floor(f[2]/2) and f[2]>1 then
  89.     f[2]=f[2]-1
  90.     s[#s+1]=f[1]^f[2]
  91.     t=t*f[1]
  92.    elseif f[2]/2 == math.floor(f[2]/2) then
  93.     s[#s+1]=f[1]^f[2]
  94.    end
  95.   end
  96.   for k2=1, #s do
  97.   S = S*math.sqrt(s[k2])
  98.   end
  99.   if t~=1 then
  100.    local ncf = ECF({b,S})
  101.    local fcf = ECF({2*a,ncf})
  102.    n1 = ((-1*b)/fcf)
  103.    n2 = (S/fcf)
  104.    d = (2*a/fcf)
  105.    if n1 == 0 then
  106.    local fcf4 = ECF({2*a,S})
  107.    n2 = S/fcf4
  108.    d = 2*a/fcf4
  109.    end
  110.   end
  111.  elseif D == 0 then
  112.  t=0
  113.  end
  114.  if t == 0 then
  115.   local fcf1 = ECF({b,2*a})
  116.   local n = b/fcf1
  117.   local da = 2*a/fcf1
  118.    if math.abs(da) ~= 1 and math.abs(da) ~= math.abs(n) then
  119.     result = "SOLUTION: "..tostring(-1*n).."/"..tostring(da).." | FACTORABLE | 1 REAL SOLUTION"
  120.    else
  121.     result = "SOLUTION: "..tostring(-1*n/da).." | FACTORABLE | 1 REAL SOLUTION"
  122.    end
  123.   elseif t>1 then
  124.   if n2 == 1 then
  125.   n2 = ""
  126.   end
  127.   if n1 ~= 0 then
  128.   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"
  129.   elseif n1 == 0 then
  130.   result = "SOLUTIONS: "..tostring(n2).."sqrt("..tostring(t)..")/"..tostring(d).." and (-"..tostring(n2).."sqrt("..tostring(t).."))/"..tostring(d).." | NOT FACTORABLE | 2 REAL SOLUTIONS"
  131.   end
  132.   elseif t<0 then
  133.   if n2 == 1 then
  134.    n2 = ""
  135.   end
  136.   t=math.abs(t)
  137.   if n1 ~= 0 then
  138.    if t ~= 1 then
  139.    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"
  140.    elseif t ==1 then
  141.    result = "SOLUTIONS: ("..tostring(n1).." + "..tostring(n2).."i)/"..tostring(d).." and ("..tostring(n1).." - "..tostring(n2).."i)/"..tostring(d).." | NO REAL SOLUTIONS"
  142.    end
  143.   elseif n1 == 0 then
  144.    if t ~= 1 then
  145.    result = "SOLUTIONS: "..tostring(n2).."sqrt("..tostring(t)..")i)/"..tostring(d).." and (-"..tostring(n2).."sqrt("..tostring(t)..")i)/"..tostring(d).." | NO REAL SOLUTIONS"
  146.    elseif t ==1 then
  147.    result = "SOLUTIONS: "..tostring(n2).."i/"..tostring(d).." and (-"..tostring(n2).."i)/"..tostring(d).." | NO REAL SOLUTIONS"
  148.    end
  149.   end
  150.  elseif t==1 then
  151.   local fcf2 = ECF({2*a,(-b)+S})
  152.   local fcf3 = ECF({2*a,b+S})
  153.   local n1a = (((-b)+S)/fcf2)
  154.   local n2a = ((b+S)/fcf3)
  155.   local db1 = (2*a/fcf2)
  156.   local db2 = (2*a/fcf3)
  157.    if math.abs(db) ~= 1 then
  158.     result = "SOLUTIONS: "..tostring(n1a).."/"..tostring(db1).." and ".."("..tostring(-1*n2a)..")/"..tostring(db2).." | FACTORABLE | 2 REAL SOLUTIONS"
  159.    else
  160.     result = "SOLUTIONS: "..tostring(n1a/db1).." and "..tostring(-1*n2a/db2).." | FACTORABLE | 2 REAL SOLUTIONS"
  161.    end
  162.  end
  163.  elseif a == 0 and b ~= 0 then
  164.  local lcf = ECF({b,c})
  165.  local ln = -c/lcf
  166.  local ld = b/lcf
  167.  if math.abs(ld) ~= 1 and math.abs(ld) ~= math.abs(c) and ln ~= 0 then
  168.  result = "SOLUTION: "..tostring(ln).."/"..tostring(ld).." | This is a linear equation."
  169.  else
  170.  result = "SOLUTION: "..tostring(ln/ld).." | This is a linear equation."
  171.  end
  172.  elseif a==0 and b == 0 then
  173.  result = "NO SOLUTIONS | This is a constant..."
  174.  if c == 0 then
  175.  result = "SOLUTION: ANY NUMBER | What equation is this?"
  176.  end
  177.  end
  178. else
  179.   result = "This program only works on integers :("
  180. end
  181. else
  182.  result = "Please check your input, it must be in the form of Solve(INTEGER,INTEGER,INTEGER)"
  183. end
  184. print(result)
  185. end
  186. --Example--------------
  187. Solve(2,3,5)
  188. ------------Remove this

Replies to Algebraic Quadratic Equation Solver rss

Title Name Language When
Re: Algebraic Quadratic Equation Solver Burly Lizard text 3 Years ago.
Re: Algebraic Quadratic Equation Solver FrancisPostsHere text 3 Years ago.