Skip to content

Instantly share code, notes, and snippets.

@WinnerOK
Created September 16, 2020 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WinnerOK/daca5ae1dc864de5faedd9f032db669a to your computer and use it in GitHub Desktop.
Save WinnerOK/daca5ae1dc864de5faedd9f032db669a to your computer and use it in GitHub Desktop.
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