I was stuck in a question which asked to find (2^n)%p where n is a very large number of the order 10^36 and p is a prime.How to do this quickly???Here ^ means power I came across this algorithm but it gives stack overflow as 10^36 is very large
double power(double a,double b,int mod)
{
if (b==0)
return 1;
else if(b%2==0)
return square(power(a,b/2,mod))%mod;
else return power(a,b-1,mod)%mod;
}
Is their any other way or an improvement on this??