# Euclidean algorithm

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?

edit retag close merge delete

( 2013-03-29 01:32:33 -0500 )edit

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

( 2013-03-30 12:12:34 -0500 )edit

Sort by ยป oldest newest most voted

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

more

( 2013-03-29 03:51:07 -0500 )edit

@fidelbc thanks !

( 2013-03-29 03:52:03 -0500 )edit

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

( 2013-03-30 12:17:26 -0500 )edit