** Fast Modular Exponentiation algorithm ** in the
Hewlett Packard HP48G language, code contributed by a generous
student.

Unfortunately, I myself do not know the exact syntax of this language, so I cannot vouch for the correctness of it.

To use the program: enter the base (X), exponent (E), and modulus (m) on the stack (in that order). Then hit the variable name the program is stored under. The result will be X^E % m << 1 -> X E M Y << DO IF E 2 MOD 0 == THEN @ Exponent is even X X * M MOD 'X' STO @ X <= X*X % m E 2 / 'E' STO @ E <= E / 2 ELSE X Y * M MOD 'Y' STO @ Y <= X*Y % m 'E' 1 STO- @ E <= E - 1 END UNTIL E 0 = @ Use "less than or equal to" character instead of = END Y >> >>