Ask Your Question
0

check if coefficients of a polynomial are divisible by a number

asked 2023-11-13 16:00:27 +0100

dc64 gravatar image

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 flag offensive close merge delete

Comments

1

You may use p.content() % n.

FrédéricC gravatar imageFrédéricC ( 2023-11-13 18:06:01 +0100 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2023-11-14 11:22:25 +0100

rburing gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2023-11-13 16:00:27 +0100

Seen: 34 times

Last updated: Nov 14