program extended_euclid_algorithm;
var
x,b,a,m,tempx,y,tempy,c,d,q,r,inverse:integer;
begin
tempx:=1;
tempy:=0;
c:=0; d:=1;
readln(a,m);
b:=m;
if(m<0) then
begin
m:=-m;
a:=-a;
end;
while(m<>0) do
begin
x:=tempx; y:=tempy;
tempx:=c; tempy:=d;
q:= a div m;
q:=-q;
c:=q*c;
d:=q*d;
c:=c+x;
d:=d+y;
r:=a mod m;
a:=m;
m:=r;
end;
writeln('the values of x and y are',tempx,' ',tempy);
writeln('the gcd is given by ',a);
if(a = -1) then
begin
a:=-a;
end;
if(a<>1) then
writeln('the modular inverese cannot be find')
else
begin
if(tempx<0) then
inverse:= (b+tempx) mod b
else
inverse:=tempx mod b;
writeln('the inverse is given by ',inverse);
end;
end.
cHJvZ3JhbSBleHRlbmRlZF9ldWNsaWRfYWxnb3JpdGhtOwp2YXIKeCxiLGEsbSx0ZW1weCx5LHRlbXB5LGMsZCxxLHIsaW52ZXJzZTppbnRlZ2VyOwpiZWdpbgoJdGVtcHg6PTE7IAoJdGVtcHk6PTA7CgljOj0wOyBkOj0xOwoJcmVhZGxuKGEsbSk7CgliOj1tOwoJaWYobTwwKSB0aGVuCgliZWdpbgoJCW06PS1tOwoJCWE6PS1hOwoJZW5kOwoJd2hpbGUobTw+MCkgZG8KCWJlZ2luIAoJCXg6PXRlbXB4OyB5Oj10ZW1weTsKCQl0ZW1weDo9YzsgdGVtcHk6PWQ7CgkJcTo9IGEgZGl2IG07CgkJcTo9LXE7CgkJYzo9cSpjOwoJCWQ6PXEqZDsKCQljOj1jK3g7CgkJZDo9ZCt5OwoJCXI6PWEgbW9kIG07CgkJYTo9bTsKCQltOj1yOwoJZW5kOwoJd3JpdGVsbigndGhlIHZhbHVlcyBvZiB4IGFuZCB5IGFyZScsdGVtcHgsJyAnLHRlbXB5KTsKCXdyaXRlbG4oJ3RoZSBnY2QgaXMgZ2l2ZW4gYnkgJyxhKTsKCWlmKGEgPSAtMSkgdGhlbgoJYmVnaW4KCQlhOj0tYTsKCWVuZDsKCWlmKGE8PjEpIHRoZW4KCXdyaXRlbG4oJ3RoZSBtb2R1bGFyIGludmVyZXNlIGNhbm5vdCBiZSBmaW5kJykKCWVsc2UKCWJlZ2luCglpZih0ZW1weDwwKSB0aGVuCglpbnZlcnNlOj0gKGIrdGVtcHgpIG1vZCBiCgllbHNlCglpbnZlcnNlOj10ZW1weCBtb2QgYjsKCXdyaXRlbG4oJ3RoZSBpbnZlcnNlIGlzIGdpdmVuIGJ5ICcsaW52ZXJzZSk7CgllbmQ7CmVuZC4K
Free Pascal Compiler version 2.2.0 [2009/11/16] for i386
Copyright (c) 1993-2007 by Florian Klaempfl
Target OS: Linux for i386
Compiling prog.pas
Linking prog
45 lines compiled, 0.0 sec
the values of x and y are-9 47
the gcd is given by 1
the inverse is given by 14