Template:Prelude/pow mod.a68: Difference between revisions

From Rosetta Code
Content added Content deleted
(This routine is used in more then one place, and is essentially a template that can by used for many different types, eg INT, LONG INT...)
 
m (than not then)
Line 1: Line 1:
COMMENT
COMMENT
This routine is used in more then one place, and is essentially a
This routine is used in more than one place, and is essentially a
template that can by used for many different types, eg INT, LONG INT...
template that can by used for many different types, eg INT, LONG INT...
USAGE
USAGE

Revision as of 23:12, 30 April 2009

COMMENT
  This routine is used in more than one place, and is essentially a
  template that can by used for many different types, eg INT, LONG INT...
USAGE
  MODE POWMODSTRUCT = INT, LONG INT, COMPL, FRAC, MODULAS, MATRIX etc
  PR READ "preclude/pow_mod.a68" PR
END COMMENT
PROC pow mod = (POWMODSTRUCT b,in e, mod)POWMODSTRUCT: (
  POWMODSTRUCT sq := b, e := in e;
  POWMODSTRUCT out:= IF ODD e THEN b ELSE 1 FI;
  e:=e OVER 2;
  WHILE e /= 0 DO
    sq *:= sq %* mod;
    IF ODD e THEN out := out * sq %* mod FI ;
    e:=e OVER 2
  OD;
  out
)