Ask Your Question
0

Euclidean algorithm

asked 12 years ago

Neda gravatar image

updated 12 years ago

vdelecroix gravatar image

Hi,

I wrote these commands in sage to have Euclidean algorithm but I only get an error.. what is the problem of this algorithm?

r=a%b
print (a,b,r)
while r != 0:
    a=b; b=r
    r=a%b
    print (a,b,r)

by the way how can I create a file named euclid.sage and save the above command in it?

Preview: (hide)

Comments

I edit your message as the code was unreadable...

vdelecroix gravatar imagevdelecroix ( 12 years ago )

Thank you so much! I'm using it with my notebook ..

Neda gravatar imageNeda ( 12 years ago )

1 Answer

Sort by » oldest newest most voted
4

answered 12 years ago

vdelecroix gravatar image

updated 12 years ago

fidbc gravatar image

Hello,

First of all, your code works! The problem is that you need to specify a value to the variables a and b. The best way should be to put all that in a function as in the following

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)

Then you can do

sage: euclide(12,5)
(12, 5, 2)
(5, 2, 1)
(2, 1, 0)

To use an external file, it depends on how do you use Sage : in a console or with the notebook ?

Vincent

Preview: (hide)
link

Comments

@vdelecroix just fixed a tiny typo in your answer

fidbc gravatar imagefidbc ( 12 years ago )

@fidelbc thanks !

vdelecroix gravatar imagevdelecroix ( 12 years ago )

thanks a lot..That was so helpful ! I'm using it with my notebook :)

Neda gravatar imageNeda ( 12 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 12 years ago

Seen: 3,090 times

Last updated: Mar 29 '13