RosettaCodeData/Task/Modular-inverse/PL-I/modular-inverse.pli

34 lines
664 B
Plaintext

*process source attributes xref or(!);
/*--------------------------------------------------------------------
* 13.07.2015 Walter Pachl
*-------------------------------------------------------------------*/
minv: Proc Options(main);
Dcl (x,y) Bin Fixed(31);
x=42;
y=2017;
Put Edit('modular inverse of',x,' by ',y,' ---> ',modinv(x,y))
(Skip,3(a,f(4)));
modinv: Proc(a,b) Returns(Bin Fixed(31));
Dcl (a,b,ob,ox,d,t) Bin Fixed(31);
ob=b;
ox=0;
d=1;
If b=1 Then;
Else Do;
Do While(a>1);
q=a/b;
r=mod(a,b);
a=b;
b=r;
t=ox;
ox=d-q*ox;
d=t;
End;
End;
If d<0 Then
d=d+ob;
Return(d);
End;
End;