how to calculate $(b^{(p \bmod m)}) \bmod m$?

90 Views Asked by At

How to calculate a base to the power an exponent which is extremely large and is already modded with a prime number m and in turn the whole expression is also modded with the same m.

1

There are 1 best solutions below

0
On BEST ANSWER

I don't know why this question is being down-voted instead of being answered.

The answer is we can't if we only know $(p\ mod\ m)$.

But if we know $p$ we could use the Fermat's Little Theorem, which says that $b^{(m-1)}\ mod\ m = 1$.

We can write $p$ as $p = q.(m-1) + r$ .

Now we can write $b^p\ mod\ m$ as $(b^{(m-1)})^{q}.b^{r}\ mod\ m$ which is nothing but $b^{(p\ mod\ (m-1))}\ mod\ m$ .