Lesson 11 of 15
Polynomial Rings
Polynomial Rings
The polynomial ring over a ring consists of all polynomials with coefficients in . We represent a polynomial as a list of coefficients: [a0, a1, a2, ...] represents .
Polynomial Arithmetic mod
Over , all coefficient arithmetic is done modulo :
def poly_add(f, g, p):
n = max(len(f), len(g))
result = [0] * n
for i in range(len(f)):
result[i] = (result[i] + f[i]) % p
for i in range(len(g)):
result[i] = (result[i] + g[i]) % p
# Strip trailing zeros
while len(result) > 1 and result[-1] == 0:
result.pop()
return result
Polynomial Multiplication mod
Standard convolution with coefficients reduced mod :
Polynomial Evaluation
To evaluate in , use Horner's method:
def poly_eval(f, a, p):
result = 0
for coeff in reversed(f):
result = (result * a + coeff) % p
return result
Your Task
Implement poly_add(f, g, p), poly_mul(f, g, p), and poly_eval(f, a, p) for polynomials over . Strip trailing zeros from results.
Pyodide loading...
Loading...
Click "Run" to execute your code.