Ask Your Question
0

Euclidean algorithm

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

Neda gravatar image

updated 2013-03-29 01:32:10 -0500

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 flag offensive close merge delete

Comments

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

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

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

Neda gravatar imageNeda ( 2013-03-30 12:12:34 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
4

answered 2013-03-29 01:37:47 -0500

updated 2013-03-29 03:50:10 -0500

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

edit flag offensive delete link more

Comments

@vdelecroix just fixed a tiny typo in your answer

fidbc gravatar imagefidbc ( 2013-03-29 03:51:07 -0500 )edit

@fidelbc thanks !

vdelecroix gravatar imagevdelecroix ( 2013-03-29 03:52:03 -0500 )edit

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

Neda gravatar imageNeda ( 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

Question Tools

Stats

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

Seen: 326 times

Last updated: Mar 29 '13