Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function Divide(DividendCoefList, DivisorCoefList)
- local DividendDegree = #DividendCoefList
- local DivisorDegree = #DivisorCoefList
- local SolutionCoefList = {}
- if DivisorDegree > DividendDegree then
- print("Degree of Divisor is Greater than that of Dividend")
- os.exit()
- else
- local SolutionDegree = DividendDegree - DivisorDegree
- for clc = DividendDegree, DivisorDegree, -1 do
- frac = DividendCoefList[clc] / DivisorCoefList[DivisorDegree]
- SolutionCoefList[SolutionDegree - (DividendDegree - clc)] = frac
- temp = {}
- for i = DivisorDegree, 0, -1 do
- temp[i] = DivisorCoefList[i] * frac
- end
- for i = clc, clc - DivisorDegree, -1 do
- DividendCoefList[i] = DividendCoefList[i] - temp[DivisorDegree - (clc - i)]
- end
- end
- return SolutionCoefList, DividendCoefList, DivisorCoefList
- end
- end
- local function printPoly(poly)
- for i = #poly, 0, -1 do
- if(poly[i] ~= nil) then
- io.write(poly[i])
- io.write(" ")
- else
- return
- end
- end
- print()
- end
- io.write("enter the degree of the Dividend:")
- local dividendD = io.read("*n")
- io.write("enter the degree of the Divisor:")
- local divisorD = io.read("*n")
- if type(dividendD) ~= "number" or type(divisorD) ~= "number" then
- print()
- print()
- print("Input must be a number")
- os.exit()
- print()
- end
- io.write("input every coefficient of the dividend polynomial in descending order from highest power:")
- local dividendCoefs = {}
- for i = dividendD, 0, -1 do
- dividendCoefs[i] = io.read("*n")
- if(type(dividendCoefs[i]) ~= "number") then
- print("Input must be a number")
- os.exit()
- end
- end
- io.write("input every coefficient of the divisor polynomial in descending order from highest power:")
- local divisorCoefs = {}
- for i = divisorD, 0, -1 do
- divisorCoefs[i] = io.read("*n")
- if(type(divisorCoefs[i]) ~= "number") then
- print("Input must be a number")
- os.exit()
- end
- end
- io.read()
- solution, remainder, divisor = Divide(dividendCoefs, divisorCoefs)
- print("the coefs of the solution are: ")
- print()
- printPoly(solution)
- print()
- print("with a remainder of: ")
- print()
- printPoly(remainder)
- io.write("divided by ")
- printPoly(divisor)
- print()
- io.write("Press enter")
- io.read()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement