Last active
December 19, 2018 10:10
-
-
Save priyankvex/8c937709a805a70b650453044048a816 to your computer and use it in GitHub Desktop.
Scamming the coding interview: Problem 007: Calculate exponenetiation
This file contains 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
""" | |
Scamming the coding interview | |
""" | |
def calculate_pow(base, exp, m): | |
if exp == 1: | |
return base | |
if m.get(exp): | |
return m.get(exp) | |
temp = int(exp/2) | |
temp_2 = exp - temp | |
ans = calculate_pow(base, temp, m) * calculate_pow(base, temp_2, m) | |
m[exp] = ans | |
return ans | |
if __name__ == '__main__': | |
num = 2 | |
exp = 10 | |
ans = calculate_pow(num, exp, {}) | |
print(ans) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment