• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Polynomial Division

Xtremecake123 Jul 12th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. local function Divide(DividendCoefList, DivisorCoefList)
2.     local DividendDegree = #DividendCoefList
3.     local DivisorDegree = #DivisorCoefList
4.     local SolutionCoefList = {}
5.     if DivisorDegree > DividendDegree then
6.         print("Degree of Divisor is Greater than that of Dividend")
7.         os.exit()
8.      else
9.         local SolutionDegree = DividendDegree - DivisorDegree
10.         for clc = DividendDegree, DivisorDegree, -1 do
11.             frac = DividendCoefList[clc] / DivisorCoefList[DivisorDegree]
12.             SolutionCoefList[SolutionDegree - (DividendDegree - clc)] = frac
13.             temp = {}
14.             for i = DivisorDegree, 0, -1 do
15.                 temp[i] = DivisorCoefList[i] * frac
16.             end
17.             for i = clc, clc - DivisorDegree, -1 do
18.                 DividendCoefList[i] = DividendCoefList[i] - temp[DivisorDegree - (clc - i)]
19.             end
20.         end
21.         return SolutionCoefList, DividendCoefList, DivisorCoefList
22.     end
23. end
24.
25.
26.
27. local function printPoly(poly)
28.     for i = #poly, 0, -1 do
29.         if(poly[i] ~= nil) then
30.           io.write(poly[i])
31.           io.write(" ")
32.         else
33.           return
34.         end
35.     end
36.     print()
37. end
38.
39.
40.
41.
42.
43. io.write("enter the degree of the Dividend:")
45. io.write("enter the degree of the Divisor:")
47.
48.
49.
50. if type(dividendD) ~= "number" or type(divisorD) ~= "number" then
51.     print()
52.     print()
53.     print("Input must be a number")
54.     os.exit()
55.     print()
56. end
57.
58. io.write("input every coefficient of the dividend polynomial in descending order from highest power:")
59.
60. local dividendCoefs = {}
61. for i = dividendD, 0, -1 do
63.     if(type(dividendCoefs[i]) ~= "number") then
64.         print("Input must be a number")
65.         os.exit()
66.     end
67. end
68.
69. io.write("input every coefficient of the divisor polynomial in descending order from highest power:")
70.
71.
72.
73. local divisorCoefs = {}
74. for i = divisorD, 0, -1 do
76.     if(type(divisorCoefs[i]) ~= "number") then
77.         print("Input must be a number")
78.         os.exit()
79.     end
80. end
82. solution, remainder, divisor = Divide(dividendCoefs, divisorCoefs)
83.
84. print("the coefs of the solution are: ")
85. print()
86. printPoly(solution)
87. print()
88. print("with a remainder of: ")
89. print()
90. printPoly(remainder)
91. io.write("divided by ")
92. printPoly(divisor)
93. print()
94. io.write("Press enter")