Skip to content

Instantly share code, notes, and snippets.

@codecakes
Last active October 14, 2019 09:15
Show Gist options
  • Select an option

  • Save codecakes/d2f698163710785e56459b9e2f32923d to your computer and use it in GitHub Desktop.

Select an option

Save codecakes/d2f698163710785e56459b9e2f32923d to your computer and use it in GitHub Desktop.
power of (number**reverse of number)
# See: https://practice.geeksforgeeks.org/problems/power-of-numbers/1/?ref=self
import functools
_MODNUM = 1000000007
# runs fine w/o lru_cache.
functools.lru_cache(maxsize=_MODNUM, typed=True)
def power(N,R):
if R == 0:
return 1
if R%2==0:
result = power(N, R//2)%_MODNUM
return (result**2)%_MODNUM
else:
return ((N%_MODNUM) * power(N, R-1))%_MODNUM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment