MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / binary_exponentiation

Function binary_exponentiation

maths/fermat_little_theorem.py:8–17  ·  view source on GitHub ↗
(a: int, n: float, mod: int)

Source from the content-addressed store, hash-verified

6
7
8def binary_exponentiation(a: int, n: float, mod: int) -> int:
9 if n == 0:
10 return 1
11
12 elif n % 2 == 1:
13 return (binary_exponentiation(a, n - 1, mod) * a) % mod
14
15 else:
16 b = binary_exponentiation(a, n / 2, mod)
17 return (b * b) % mod
18
19
20# a prime number

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected