Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How can you define a function that finds the Greatest Common Divisor (Gcd) two polynomials for every field?

Hi, as the title says I`m trying to define a function that finds the gcd of two polynomial without using the pre-established function gcd. I've tried everything I thought it would work:

First, I tried to use the Euclidan Algorithm, for that you need to divide the polynomials. Knowing so, I tried to find the degrees of the different polynomials to divide in consequence of the degrees (which gave me error). Then I tried it without the degree part and it didn't work at all since % couldn't be used as a divisor of polynomials.

def GCD(field, f, g):
  R.<x> = PolynomialRing(field, 'x')
  x.parent() 
  a = f.degree()
  b = g.degree()
  if a>b:
    while g != 0: 
        r = g
        g = f%g
  else:
    while f != 0:
        r = g
        f = g%f
  return r

Shortly after I tried to factor both of the polynomial and make the funciĆ³n return the part that repeated. But I rapidly saw my hopes decay when I realized I have not a single clue in how to do so (even though I've done some research I couldn't find the answer).

def mcd(field, f, g): 
  R.<x> = PolynomialRing(field) 
  a = f.factor()
  b = g.factor()

And this was the last code I wrote before asking for some enlightening:

def MCD(Field,PolynomialA, PolinomialB):
  R.<x> = PolynomialRing(Field, 'x')
  a = PolynomialA
  b = PolynomialB
  c = 1
  while c != 0:
    c = a%b 
    a = b
    b = c
  return a