Berlekamp’s algorithm gap> x:=X(GF(3),"x");; f:=x^9+x^8-x^6-x^5-x^4-x^3+Z(3)^0;; Calculate the powers of xq and store the coefficients in a matrix. gap> xq:=PowerMod(x,q,f);; gap> pows:=List([0..8],i->PowerMod(xq,i,f)); [ Z(3)^0, x^3, x^6, -x^8+x^6+x^5+x^4+x^3-Z(3)^0, x^7+x^6+x^4-x^3+x^2-x, -x^7-x^5-x, x^8-x^7-x^4+x^3+x-Z(3)^0, -x^8+x^7-x^6+x^5+x^4+x^3-x^2-x+Z(3)^0, -x^7-x^6+x^4+x^3+x^2+x ] gap> qmat:=List(pows,CoefficientsOfUnivariatePolynomial); [ [ Z(3)^0 ], [ 0*Z(3), 0*Z(3), 0*Z(3), Z(3)^0 ], ... The rows are not equal lengths, we have to extend the shorter ones. As they are immutable, we have to make a copy first gap> [ 1, gap> gap> List(qmat,Length); 4, 7, 9, 8, 8, 9, 9, 8 ] qmat:=List(qmat,ShallowCopy);; for i in qmat do while Length(i)<9 do Add(i,0*Z(3));od;od; Q now is quadratic, as we could check with Display gap> Display(qmat); 1 . . . . . . . . ... Now create an identity matrix and consider Q − I. gap> id:=IdentityMat(9,Z(3)^0);; gap> RankMat(qmat-id); 6 Rank 6 = 9 − 3 implies: 3 factors. Calculate a basis of the nullspace. (GAPs linear algebra works on row vectors, the nullspace thus is the left null space. From the coefficient lists, we go back to polynomials. gap> bas:=NullspaceMat(qmat-id); [ [ Z(3)^0, 0*Z(3), 0*Z(3), 0*Z(3), 0*Z(3), 0*Z(3), 0*Z(3), 0*Z(3), 0*Z(3) ], . <3 vectors total >.. gap> bpols:=List(bas,i->UnivariatePolynomial(GF(3),i,1)); [ Z(3)^0, x^7+x^6-x^5-x^2+x, x^8-x^6-x^5-x^4+x^3-x ] We now create a random nullspace element. It happens to have nontrivial gcd with f , so we got a factor. gap> a:=Sum([1,2,3],i->Random(GF(3))*bpols[i]); x^8-x^7+x^6-x^4+x^3+x^2+x+Z(3)^0 gap> g:=Gcd(a,f); x^3+x^2-Z(3)^0 gap> f:=f/g; x^6-x^2-Z(3)^0 The next a has trivial gcd. We thus calculate b = a(q−1)/2 mod f and form the gcd with b − 1. This turns out to give a factor. gap> a:=Sum([1,2,3],i->Random(GF(3))*bpols[i]);; gap> Gcd(a,f); Z(3)^0 gap> b:=PowerMod(a,(q-1)/2,f);; gap> Gcd(b-1,f); x^3+x^2-x+Z(3)^0