Ask Your Question

Revision history [back]

In Python (the programming language Sage uses),

  • assignment uses =, while == is to test equality
  • indentation is syntax, and in particular
  • the content of a while loop is everything that is indented after :.

So your code might want to look something like that instead:

def div(f, g):
    q = 0
    r = f
    while r != 0 and g.lt().divides(r.lt()):
        q = q + r.lt() // g.lt()
        r = r - (r.lt() // g.lt()) * g
    return q, r

Then you can use the function as follows.

Define a polynomial ring and two polynomials:

sage: R.<x> = QQ[]
sage: f = x^5 + x^3 + 2
sage: g = x^2 + 1

Apply the function:

sage: div(f, g)
(x^3, 2)