RosettaCodeData/Task/Modular-exponentiation/Seed7/modular-exponentiation-2.seed7
Ingy döt Net 6f050a029e update
2013-06-05 21:47:54 +00:00

17 lines
474 B
Text

const func bigInteger: modPow (in var bigInteger: base,
in var bigInteger: exponent, in bigInteger: modulus) is func
result
var bigInteger: power is 1_;
begin
if exponent < 0_ or modulus < 0_ then
raise RANGE_ERROR;
else
while exponent > 0_ do
if odd(exponent) then
power := (power * base) mod modulus;
end if;
exponent >>:= 1;
base := base ** 2 mod modulus;
end while;
end if;
end func;