Stack Exchange
division modulo 2 in python 3 ,0
I need to correct the following code:
This program divides into K [x] (K = {0,1}), making module 2
in every arithmetic operation. Here the polynomials are taken
as lists, for example: x ^ 2 + x + 1 is [1,1,1]
Example, if the polynomial [1,1,1,0,0,0,1,1,0] is divided by
[1,0,1,1,1], the result is:
Residue: [1,1,1,0]
Ratio: [1,1,0,0,0]

p= [1,1,1,0,0,0,1,1,0]
q = [1,0,1,1,1]
c = []
r = []

while len(p)<len(q):
    c.append((q[0]/p[0]))
    for i in range(len(q)):
        for j in range(len(p)):
            if len(q)-1-i != len(p)-1-j + c[len(c)-1][1]:
                q[i]=c[len(c)-1][0]*p[j]
    for j in range(len(q)):
        r.append(q[i])
    q = r
    r=[]


print("El residuo es: ", q)

print("El cociente es: ", c)



This is the standard division algorithm for polynomials, but over integers mod 2 instead of the usual integers.  Just take the standard algorithm and replace each arithmetic operation by the corresponding one mod 2.
One thing to keep in mind, assuming that the users get to enter arbitrary p and q:  Can you be sure that the coefficients of p and q are taken from {0,1}?  After all, x^2+x+1 = 3x^2-x+7 (mod 2).  If the coefficients could be general integers (but viewed mod 2) then e.g. the line
c.append((q[0]/p[0]))
needs to be modified accordingly.
