Ask Your Question

# 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

## Comments

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

( 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

## 1 answer

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

## Comments

@vdelecroix just fixed a tiny typo in your answer

( 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

## Your Answer

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

Add Answer

## Stats

Asked: 2013-03-29 01:23:51 -0500

Seen: 1,322 times

Last updated: Mar 29 '13