# Powers of irreducible polynomials

I'm working on a WeBWorK project to demonstrate the relatively new WeBWorK-Sage connectivity. We'd like to demonstrate some math problems that one could program in WeBWorK using Sage more cleanly than without Sage.

To that end I'm trying the following: WeBWorK would display a randomly generated polynomial in Z[x]. Let's say it could be degree 2, 3, or 4 with integer coefficients from [-10,10]. A student has to factor it completely.

I'm able to send the student's answer to Sage as a string, properly formatted for Sage. It might be '(x^2+1)(2x+1)' or '2(x^2+3x+2)*(x+2)^2'. But how can I evaluate whether or not the answer is fully factored? (For the moment, I'm not concerned about factoring out integers.)

So far, I have done this, where poly is the string passed to Sage:


factored = True
if (poly).operator()!=operator.mul:
if (poly).polynomial(base_ring=QQ).is_irreducible():
factored = True
else:
factored = False
else:
factors = (poly).operands()
for factor in factors:
factored = factored and factor.polynomial(base_ring=QQ).is_irreducible()
print factored
 

And factored is True if all of the factors in poly were irreducible, and False otherwise. This is not what I want since I want something like '(x+1)^2*(x+2)' to count as factored. Is there a quick test for whether or not a polynomial is a power of an irreducible? Or a constant multiple of such a thing?

edit retag close merge delete

Sort by ยป oldest newest most voted

sage: R.<x> = PolynomialRing(QQ, 'x')
sage: P = (1/2*x+3)^5
sage: P.factor()
(1/32) * (x + 6)^5
sage: len(P.factor())
1
sage: Q = P * (3*x+2)
sage: len(Q.factor())
2


This is because you may think of the result of .factor() as a unit together with a list of pairs (factor, multiplicity).

more