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

Replies to Re: Algebraic Quadratic Equation Solver rss

Title Name Language When
Re: Re: Algebraic Quadratic Equation Solver Tinct Bushbaby text 1 Year ago.