RosettaCodeData/Task/Modular-exponentiation/Python/modular-exponentiation-2.py

18 lines
406 B
Python

def power_mod(b, e, m):
" Without using builtin function "
x = 1
while e > 0:
b, e, x = (
b * b % m,
e // 2,
b * x % m if e % 2 else x
)
return x
a = 2988348162058574136915891421498819466320163312926952423791023078876139
b = 2351399303373464486466122544523690094744975233415544072992656881240319
m = 10 ** 40
print(power_mod(a, b, m))