Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Simple square-multiply exponentiation
base, exp, mod = list(map(int, input("Enter base power mod: ").split()))
r = 1
for i in bin(exp)[2:]:
bit = int(i)
r = (r * r) % mod
if bit == 1:
r = (r * base) % mod
print(f"Answer: {r}\nr == (base ** exp) % mod: {r == (base ** exp) % mod}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment