Search code examples
listgreatest-common-divisorsage

Euclidean algorithm on Sage for more than 2 elements


I'm trying to make an exercise which gets a list of numers, an shows a list of elements like this: if A=[a0,a1,a2] then there is U=[u0,u1,u2], knowing that a0*u0 + a1*u1 + a2*u2 = d and d is the gcd of A.

For 2 elements is a pretty simple thing, as Sage has a function to retrieve u0 and u1 out of a0 and a1:

A=[15,21]
(d,u0,u1)=xgcd(a[0],a[1])

I just don't understand how could I do this with a list of n elements.


Solution

  • You helped me a lot, came to this:

    x1=[1256,5468,5552,1465]
    n=-1
    for i in x1:
        n=n+1
    (d,w,x)=xgcd(x1[n-1],x1[n])
    u1=[w,x]
    n=n-2
    while n>=0:
        div=d
        (d,u,v)=xgcd(x1[n],div)
        position=0
        for j in u1:
            a=j*v
            u1[position]=a
            position=position+1
        u1=[u]+u1
        n=n-1
    u1
    

    And it works ;)