program eualdem1; {demonstrate the Euclidean Algorithm} {$N+,E+} uses CRT; {$I getinput.i } const MaxAllow = 1E18-1; {largest number that will be allowed} var t {quotient as a real number} : extended; b, {first input} c, {second input} q, {quotient in division algorithm} r {remainder in division algorithm} : comp; x0, y0 {cursor coordinates} : integer; Ch {character read from keyboard} : char; begin ClrScr; WriteLn; WriteLn('Demonstration of the calculation of the greatest common divisor'); Write('(b, c) by means of the Euclidean Algorithm. We use the four '); WriteLn('identities:'); WriteLn(' i) (b, c) = (-b, c);'); WriteLn(' ii) (b, c) = (b + mc, c) for all integers m;'); WriteLn(' iii) (b, c) = (c, b);'); WriteLn(' iv) (b, 0) = |b|.'); WriteLn; b := GetInput(WhereX, WhereY, ' Enter b = ', 'where ³b³ ó 999999999999999999', -MaxAllow+1, MaxAllow-1); c := GetInput(WhereX, WhereY, ' Enter c = ', 'where ³c³ ó 999999999999999999', -MaxAllow+1, MaxAllow-1); GoToXY(1, WhereY-1); ClrEoL; GoToXY(1, WhereY - 1); ClrEoL; if (b = 0) and (c = 0) then begin WriteLn('(0, 0) is undefined.'); WriteLn; halt end; Write('(', b:1:0, ', ', c:1:0, ')'); x0 := WhereX; if b < 0 then {replace b by ³b³} begin b := -b; Write(' = (', b:1:0, ', ', c:1:0, ')'); GoToXY(68, WhereY); WriteLn('by i)'); y0 := WhereY; GoToXY(20, 25); Write('Press any key to continue . . . '); Ch := ReadKey; GoToXY(1, 25); ClrEoL; GoToXY(x0, y0) end; if c < 0 then {replace c by ³c³} begin c := -c; Write(' = (', b:1:0, ', ', c:1:0, ')'); GoToXY(68, WhereY); WriteLn('by i) & iii)'); y0 := WhereY; GoToXY(20, 25); Write('Press any key to continue . . . '); Ch := ReadKey; GoToXY(1, 25); ClrEoL; GoToXY(x0, y0) end; if b < c then {swap b and c} begin t := b; b := c; c := t; Write(' = (', b:1:0, ', ', c:1:0, ')'); GoToXY(68, WhereY); WriteLn('by iii)'); y0 := WhereY; GoToXY(20, 25); Write('Press any key to continue . . . '); Ch := ReadKey; GoToXY(1, 25); ClrEoL; GoToXY(x0, y0) end; y0 := WhereY; while c <> 0 do begin t := b/c; {quotient as a real number} q := t; {nearest integer} if q > t then q := q - 1; {force round down} r := b - q*c; {b = qc + r; the division algorithm} Write(' = (', b:1:0, ' - ', q:1:0, 'ù', c:1:0, ', ', c:1:0, ')'); GoToXY(68, WhereY); WriteLn('by ii)'); GoToXY(x0, WhereY); WriteLn(' = (', r:1:0, ', ', c:1:0, ')'); GoToXY(x0, WhereY); b := c; c := r; Write(' = (', b:1:0, ', ', c:1:0, ')'); GoToXY(68, WhereY); WriteLn('by iii)'); y0 := WhereY; GoToXY(1, 25); while y0 >= 24 do {insert blank lines at bottom of screen} begin WriteLn; y0 := y0-1 end; GoToXY(20, 25); Write('Press any key to continue . . . '); Ch := ReadKey; GoToXY(1, 25); ClrEoL; GoToXY(x0, y0); end; Write(' = ', b:1:0); GoToXY(68, WhereY); WriteLn('by iv)'); WriteLn end.