%%time
def pow(x,n):
hash_map={0:0,1:x}
def helper(x,n):
if n not in hash_map:
hash_map[n]=helper(x,n//2)*helper(x,n-n//2)
return hash_map[n]
return helper(x,n) if n>=0 else 1/helper(x,-n)
for i in range(1000):
pow(2,100000)
%%time
def pow(x,n):
def helper(x,n):
if n==0:
return 1
elif n%2==0:
return helper(x*x,n//2)
else:
return x*helper(x,n-1)
return helper(x,n) if n>=0 else 1/helper(x,-n)
for i in range(1000):
pow(2,100000)