1

I am working on the question below, which I have copied from a PDF file. I have determined what the program does (Euclidian algorithm), but I need helping running the actual program. I am instructed to use the "text insertion" feature. I am using Maple 16 for Mac. I go to Insert:Text and then paste the given code (I've included this below) in the Workspace. If I select the code and then use Edit:Execute:Selection I get errors saying "unable to match delimiter". Any help with how to execute this program and use my own inputs would be greatly appreciated. Do the number of spaces for blocking the code matter, etc?

Picture of question from PDF

Code (without any line indents):

euclid:= proc(m,n) local q,r1,r2,r3;
r1:=m; r2:=n;
while r2 <> 0 do;
q:=iquo(r1,r2); r3:= irem(r1,r2);
lprint(r1, ‘=‘, q, ‘*‘, r2, ‘+‘, r3);
r1:=r2; r2:=r3; od;
RETURN(r1); end;
Malthus
  • 353

2 Answers2

1

Something went awry when you copied from the pdf. The quotation marks are incorrect. Try replacing them with " or `. Typographic quotation marks won't work.


Added: Try copy-pasting this:

euclid := proc (m, n) 
local q, r1, r2, r3; 
r1 := m; r2 := n; 
while r2 <> 0 do;
q := iquo(r1, r2); r3 := irem(r1, r2); 
lprint(r1, "=", q, "*", r2, "+", r3); 
r1 := r2; r2 := r3; 
od; 
RETURN(r1) 
end;

This works for me and gives for example:

> euclid(48, 30);
48, "=", 1, "*", 30, "+", 18
30, "=", 1, "*", 18, "+", 12
18, "=", 1, "*", 12, "+", 6
12, "=", 2, "*", 6, "+", 0
                               6
mrf
  • 43,639
  • Thank you for your reply! I've fixed the quotation marks, and then simply pasted it in as Maple input (and hit 'enter') and I still receive a mismatched delimiter error. Do I need to indent the lines in a specific way (like in Python)? – Malthus Oct 02 '13 at 23:16
  • Here is a screen shot for reference: http://i.imgur.com/gl8oNx1.png – Malthus Oct 02 '13 at 23:17
  • @Malthus Did you remember to change all six of them? It worked for me. Indentation is not important. – mrf Oct 02 '13 at 23:17
  • I fixed all 6 - still the same result unfortunately. Could you walk me through your procedure of getting it to work? That would be excellent. Thanks for your help regardless. – Malthus Oct 02 '13 at 23:49
0

As others have noted, the issue is with the single quotes. The use of the all-caps RETURN has also been deprecated in Maple for about 15 years, though it still works.

As an efficiency improvement, you can compute the remainder and quotient simultaneously by passing in a quoted third argument to either iquo or irem, and you can assign values to multiple variables simultaneously. So the following should work:

euclid := proc(m, n) local q,r1,r2,r3;
    r1, r2 := m, n; 
    while r2 <> 0 do
        q := iquo(r1, r2, 'r3');
        lprint(r1, "=", q, "*", r2, "+", r3); 
        r1, r2 := r2, r3;
    od; 
    r1
end proc:
saforrest
  • 116