This commit is contained in:
0
src/holt59/aoc/tools/__init__.py
Normal file
0
src/holt59/aoc/tools/__init__.py
Normal file
21
src/holt59/aoc/tools/math.py
Normal file
21
src/holt59/aoc/tools/math.py
Normal 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
|
Reference in New Issue
Block a user