Created
October 17, 2013 13:45
-
-
Save shobhit6993/7025165 to your computer and use it in GitHub Desktop.
Simple recursive function for exponentiation in O(log(n)) time with Mod.
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
//recursive version | |
int64_t powMod(int64_t base,int64_t expo) | |
{ | |
if(expo==0) | |
{ | |
return 1; | |
} | |
else if(expo%2==0) | |
{ | |
int64_t ans=powMod(base,expo/2); | |
return ans*ans%MOD; | |
} | |
else | |
{ | |
return base*powMod(base,expo-1)%MOD; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment