Ask Your Question
0

Euclidean algorithm

asked 2013-03-29 07:23:51 +0200

Neda gravatar image

updated 2013-03-29 07:32:10 +0200

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?

edit retag flag offensive close merge delete

Comments

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

vdelecroix gravatar imagevdelecroix ( 2013-03-29 07:32:33 +0200 )edit

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

Neda gravatar imageNeda ( 2013-03-30 18:12:34 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted
4

answered 2013-03-29 07:37:47 +0200

vdelecroix gravatar image

updated 2013-03-29 09:50:10 +0200

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 09:51:07 +0200 )edit

@fidelbc thanks !

vdelecroix gravatar imagevdelecroix ( 2013-03-29 09:52:03 +0200 )edit

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

Neda gravatar imageNeda ( 2013-03-30 18:17:26 +0200 )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 07:23:51 +0200

Seen: 2,735 times

Last updated: Mar 29 '13