Ask Your Question

Revision history [back]

To complement @kcrisman's answer: the code you gave looks like a "debugging" version, with the print statements.

I suppose you would want to comment out the print statements and make the function return the result.

def euclide(a,b):
    r=a%b
    # print (a,b,r)
    while r !=0:
        a=b; b=r
        r=a%b
        # print (a,b,r)
    return b

Then you would get:

sage: euclide(12,5)
1

Or you could introduce an extra (optional) argument to your function to decide whether to print steps.

def euclide(a,b,verbose=False):
    r=a%b
    if verbose:
        print (a,b,r)
    while r !=0:
        a=b; b=r
        r=a%b
        if verbose:
            print (a,b,r)
    return b

Then you could do:

sage: euclide(12,5) 
1
sage: euclide(12,5,verbose=True)
(12, 5, 2)
(5, 2, 1)
(2, 1, 0)
1