RosettaCodeData/Task/Modular-exponentiation/Oforth/modular-exponentiation.fth

7 lines
206 B
Forth
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
: powmod(base, exponent, modulus)
1 exponent dup ifZero: [ return ]
while ( dup 0 > ) [
dup isEven ifFalse: [ swap base * modulus mod swap ]
2 / base sq modulus mod ->base
] drop ;