Shared2018-05-04-113300.sagewsOpen in CoCalc
SI - Homework4
def _gcd(a, b):
    """
    Returns the greatest common divisor of a and b.
    """
    if b == 0:
        #show('2')
        return a
    else:
        quociente, resto = (a).quo_rem(b)
        if resto == 0:
            #show('3')
            return b
        show(a, '= (', b,')*(', quociente,')+(', resto,')')
        return _gcd(b, resto.monic())

def  my_xgcd(n, m):
    if n == 0:
        return (m, 0, 1) if m  >= 0 else (-m, 0,  -1)
    else:
        g, x, y = my_xgcd(m % n, n)
        return (g, y - (m // n) * x, x)

R = PolynomialRing(ZZ, 'x')
R1.<t> = GF(5)[]

lista1 = [3, 1, 0, 1]
lista2 = [7, 23, 0, 0, 0, 1]

poly1 = R(lista1)
poly2 = R1(lista2)
poly3 = R1.random_element(degree=6)
poly4 = 3*x**4 + 5*x**2 + 3

poly1.is_irreducible() #True or False
poly2.factor() #factorization

#show(poly1)
show(poly2)
show(poly3)
#show(poly4)
#show(gcd(poly2, poly3))
show(xgcd(poly2, poly3))
show(my_xgcd(poly2, poly3))