Skip to content

Instantly share code, notes, and snippets.

@shobhit6993
Created October 17, 2013 13:45
Show Gist options
  • Save shobhit6993/7025165 to your computer and use it in GitHub Desktop.
Save shobhit6993/7025165 to your computer and use it in GitHub Desktop.
Simple recursive function for exponentiation in O(log(n)) time with Mod.
//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