Skip to content

Instantly share code, notes, and snippets.

@priyankvex
Last active December 19, 2018 10:10
Show Gist options
  • Save priyankvex/8c937709a805a70b650453044048a816 to your computer and use it in GitHub Desktop.
Save priyankvex/8c937709a805a70b650453044048a816 to your computer and use it in GitHub Desktop.
Scamming the coding interview: Problem 007: Calculate exponenetiation
"""
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