2015 day 25.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Mikaël Capelle
2024-12-14 21:02:23 +01:00
parent 323f810fcd
commit 91ba8ec86f
5 changed files with 39 additions and 0 deletions

View File

View File

@@ -0,0 +1,21 @@
def pow_mod(b: int, e: int, m: int):
"""
Compute (b ** e) % m using right-to-left binary method.
See https://en.wikipedia.org/wiki/Modular_exponentiation.
Args:
b: Base to exponentiate.
e: Exponent.
m: Modulus.
Returns:
(b ** e) % m.
"""
r = 1
while e > 0:
if e % 2 == 1:
r = (r * b) % m
e >>= 1
b = (b * b) % m
return r