Created
October 22, 2017 10:26
-
-
Save rajatdiptabiswas/a8a7228a79fe8e0c0e4bd33e0f645368 to your computer and use it in GitHub Desktop.
Exponentiation by Squaring Algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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