Skip to content

Instantly share code, notes, and snippets.

@rajatdiptabiswas
Created October 22, 2017 10:26
Show Gist options
  • Save rajatdiptabiswas/a8a7228a79fe8e0c0e4bd33e0f645368 to your computer and use it in GitHub Desktop.
Save rajatdiptabiswas/a8a7228a79fe8e0c0e4bd33e0f645368 to your computer and use it in GitHub Desktop.
Exponentiation by Squaring Algorithm
#!/usr/bin/env python3
def square_expo(x, n):
if n == 0:
return 1;
if n % 2 == 0:
return square_expo(x * x, n // 2)
return x * square_expo(x * x, n // 2)
x = int(input("x = "))
n = int(input("n = "))
print(square_expo(x, n))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment