# check if coefficients of a polynomial are divisible by a number

I would like to make a program that take as an entry a given integer coefficient polynomial and check if the coefficients of this polynomial are all divisible by a given number n.
How could I do it ?
(sorry if the question is stupid but I could not do it in python and I discovered SageMath and it looked like it was kind of easier to do it with it)

edit retag close merge delete

1

Sort by » oldest newest most voted You can do it like this:

sage: R.<x> = PolynomialRing(ZZ)
sage: n = 3
sage: f = x^3 + 1
sage: all(n.divides(c) for c in f.coefficients())
False
sage: g = 3*f
sage: all(n.divides(c) for c in g.coefficients())
True


The same approach also works for multivariate polynomials:

sage: R.<x,y> = PolynomialRing(ZZ)
sage: n = 3
sage: f = x^3 + y^3
sage: all(n.divides(c) for c in f.coefficients())
False
sage: g = 3*f
sage: all(n.divides(c) for c in g.coefficients())
True


Instead of n.divides(c) (assuming n is a SageMath Integer, as it is above) you could also use c % n == 0.

more