You can easily do this in a calculator by dividing the exponent into pieces.
For example: Let's say we have to calculate .
Divide 23 into as many pieces as you want, for example .
The bigger the exponent, more the numbers you should divide it in.
Now, calculate
,
and
(All three of which can easily be calculated using your method in any calculator).
Now multiply all the three values, , and take its mod with
again. The final answer will give you your answer.
So, $112 x 118 x 118 = 1559488$
$1559488\ mod\ 221 = 112$ which is the correct answer for .
How to calculate the modulus of a big number on a calculator? - Mathematics Stack Exchange
math - How to calculate modulus of large numbers? - Stack Overflow
modular arithmetic - How do you calculate the modulo of a high-raised number? - Mathematics Stack Exchange
Does anyone have a recommended online modulus calculator that works with very large numbers?
How to calculate modulo division?
To calculate modulo division: subtract the divisor from the dividend until the resultant is less than the divisor.
How do I calculate exponential modulo?
If the numbers at hand are not very big, you can simply solve the exponent first and then apply the modulo. Otherwise, you either need to apply some smart reasoning, a math theorem (like Fermat's little theorem or Euler's theorem), or a specialized computer algorithm that performs fast modular exponentiation.
What is a modulo operator?
The modulo operator is used to find the remainder during a division of two numbers. The operator is represented by the symbol % in most programming languages. It is also known as the remainder operator. As an example, 5 mod 2 returns 1.
Videos
Okay, so you want to calculate a^b mod m. First we'll take a naive approach and then see how we can refine it.
First, reduce a mod m. That means, find a number a1 so that 0 <= a1 < m and a = a1 mod m. Then repeatedly in a loop multiply by a1 and reduce again mod m. Thus, in pseudocode:
a1 = a reduced mod m
p = 1
for(int i = 1; i <= b; i++) {
p *= a1
p = p reduced mod m
}
By doing this, we avoid numbers larger than m^2. This is the key. The reason we avoid numbers larger than m^2 is because at every step 0 <= p < m and 0 <= a1 < m.
As an example, let's compute 5^55 mod 221. First, 5 is already reduced mod 221.
1 * 5 = 5 mod 2215 * 5 = 25 mod 22125 * 5 = 125 mod 221125 * 5 = 183 mod 221183 * 5 = 31 mod 22131 * 5 = 155 mod 221155 * 5 = 112 mod 221112 * 5 = 118 mod 221118 * 5 = 148 mod 221148 * 5 = 77 mod 22177 * 5 = 164 mod 221164 * 5 = 157 mod 221157 * 5 = 122 mod 221122 * 5 = 168 mod 221168 * 5 = 177 mod 221177 * 5 = 1 mod 2211 * 5 = 5 mod 2215 * 5 = 25 mod 22125 * 5 = 125 mod 221125 * 5 = 183 mod 221183 * 5 = 31 mod 22131 * 5 = 155 mod 221155 * 5 = 112 mod 221112 * 5 = 118 mod 221118 * 5 = 148 mod 221148 * 5 = 77 mod 22177 * 5 = 164 mod 221164 * 5 = 157 mod 221157 * 5 = 122 mod 221122 * 5 = 168 mod 221168 * 5 = 177 mod 221177 * 5 = 1 mod 2211 * 5 = 5 mod 2215 * 5 = 25 mod 22125 * 5 = 125 mod 221125 * 5 = 183 mod 221183 * 5 = 31 mod 22131 * 5 = 155 mod 221155 * 5 = 112 mod 221112 * 5 = 118 mod 221118 * 5 = 148 mod 221148 * 5 = 77 mod 22177 * 5 = 164 mod 221164 * 5 = 157 mod 221157 * 5 = 122 mod 221122 * 5 = 168 mod 221168 * 5 = 177 mod 221177 * 5 = 1 mod 2211 * 5 = 5 mod 2215 * 5 = 25 mod 22125 * 5 = 125 mod 221125 * 5 = 183 mod 221183 * 5 = 31 mod 22131 * 5 = 155 mod 221155 * 5 = 112 mod 221
Therefore, 5^55 = 112 mod 221.
Now, we can improve this by using exponentiation by squaring; this is the famous trick wherein we reduce exponentiation to requiring only log b multiplications instead of b. Note that with the algorithm that I described above, the exponentiation by squaring improvement, you end up with the right-to-left binary method.
a1 = a reduced mod m
p = 1
while (b > 0) {
if (b is odd) {
p *= a1
p = p reduced mod m
}
b /= 2
a1 = (a1 * a1) reduced mod m
}
Thus, since 55 = 110111 in binary
1 * (5^1 mod 221) = 5 mod 2215 * (5^2 mod 221) = 125 mod 221125 * (5^4 mod 221) = 112 mod 221112 * (5^16 mod 221) = 112 mod 221112 * (5^32 mod 221) = 112 mod 221
Therefore the answer is 5^55 = 112 mod 221. The reason this works is because
55 = 1 + 2 + 4 + 16 + 32
so that
5^55 = 5^(1 + 2 + 4 + 16 + 32) mod 221
= 5^1 * 5^2 * 5^4 * 5^16 * 5^32 mod 221
= 5 * 25 * 183 * 1 * 1 mod 221
= 22875 mod 221
= 112 mod 221
In the step where we calculate 5^1 mod 221, 5^2 mod 221, etc. we note that 5^(2^k) = 5^(2^(k-1)) * 5^(2^(k-1)) because 2^k = 2^(k-1) + 2^(k-1) so that we can first compute 5^1 and reduce mod 221, then square this and reduce mod 221 to obtain 5^2 mod 221, etc.
The above algorithm formalizes this idea.
To add to Jason's answer:
You can speed the process up (which might be helpful for very large exponents) using the binary expansion of the exponent. First calculate 5, 5^2, 5^4, 5^8 mod 221 - you do this by repeated squaring:
5^1 = 5(mod 221)
5^2 = 5^2 (mod 221) = 25(mod 221)
5^4 = (5^2)^2 = 25^2(mod 221) = 625 (mod 221) = 183(mod221)
5^8 = (5^4)^2 = 183^2(mod 221) = 33489 (mod 221) = 118(mod 221)
5^16 = (5^8)^2 = 118^2(mod 221) = 13924 (mod 221) = 1(mod 221)
5^32 = (5^16)^2 = 1^2(mod 221) = 1(mod 221)
Now we can write
55 = 1 + 2 + 4 + 16 + 32
so 5^55 = 5^1 * 5^2 * 5^4 * 5^16 * 5^32
= 5 * 25 * 625 * 1 * 1 (mod 221)
= 125 * 625 (mod 221)
= 125 * 183 (mod 183) - because 625 = 183 (mod 221)
= 22875 ( mod 221)
= 112 (mod 221)
You can see how for very large exponents this will be much faster (I believe it's log as opposed to linear in b, but not certain.)
There are often tricks to this if the numbers are nice enough, but even if they're not, here's a way that's not entirely horrible.
You already know what 439 is mod 713. What is $439^2 \mod 713$? What about $439^4$? (Hint: take your answer for $439^2$ after reducing it mod 713, and then square it again.) In the same way, calculate $439^8, 439^{16}, \dots, 439^{128} \mod 713$. Now just note that 233 = 128 + 64 + 32 + 8 + 1. So multiply the appropriate powers of 439 together - again, one calculation at a time, reducing mod 713 each time.
Now you should only have to do 11 calculations, and now all your numbers are 6 digits or less. Rather than impossible, it's now simply tedious. :)
By the way, one thing to notice: 713 = 23 * 31. Perhaps your calculations will be easier if you do them mod 23 and 31, then apply the Chinese remainder theorem?
$713=23\cdot 31$
$439 \pmod {23}=2$ and $\phi(23)=22$ and $233\equiv 13{\pmod {22}}$
So, $439^{223} {\pmod {23}} \equiv 2^{22\cdot 10 + 13}\equiv {(2^{22})}^{10}2^{13}\equiv 2^{13} {\pmod {23}}$ using Euler's Totient Theorem.
$2^6\equiv 18 {\pmod {23}}, 2^7\equiv 36 \equiv 13$
$\implies 2^{13}\equiv 18\cdot 13=234\equiv 4 {\pmod {23}}=4+23x$ for some integer $x$.
$439 \pmod {31}=5$ and $\phi(31)=30$ and $233\equiv 23{\pmod {30}}$
So, $439^{223} {\pmod {31}} \equiv 5^{23} {\pmod {31}}$
$5^3 \equiv 1 {\pmod {31}} \implies 5^{23}\equiv({5^3})^7 5^2\equiv 5^2{\pmod {31}}=25+31y$ for some integer $y$.
So, we need to find $z$ such that $z=25+31y=4+23x$
Expressing as continued fraction, $$\frac{31}{23}=1+\frac{8}{23}=1+\frac{1}{\frac{23}{8}}=1+\frac{1}{2+\frac{7}{8}}$$
$$=1+\frac{1}{2+\frac{1}{\frac{8}{7}}}=1+\frac{1}{2+\frac{1}{1+\frac{1}{7}}}$$
So, the last but one convergent is $$1+\frac{1}{2+\frac{1}{1}}=\frac{4}{3}$$
So, $23\cdot 4- 31\cdot 3=-1$
$25+31y=4+23x\implies 23x=31y+21(31\cdot 3-23\cdot 4)$ $\implies 23(x+84)=31(y+63)$ $$\implies x+84=\frac{31(y+63)}{23}$$
So, $23\mid (y+63)$ as $x+84$ is integer and $(31,23)=1$ i.e., $ 23\mid (y+69-6)\implies 23\mid (y-6) \implies y=6+23w$
So, $z=25+31y=25+31(6+31w)=713w+211 \equiv 211 {\pmod {713}}$
For example, 152015^(154993) mod 262631