programming-examples/python/Algorithms/binary_exponentiation.py

17 lines
283 B
Python
Raw Normal View History

2019-11-15 12:59:38 +01:00
# https://en.wikipedia.org/wiki/Exponentiation_by_squaring
def pow(a, b, mod):
res = 1
while b > 0:
if b & 1 != 0:
res = res * a % mod
a = a * a % mod
b >>= 1
return res
def test():
print(1024 == pow(2, 10, 1000000007))
test()