How to calculate modulus of 5^55 modulus 221 without much use of calculator?
I guess there are some simple principles in number theory in cryptography to calculate such things.
How to calculate modulus of 5^55 modulus 221 without much use of calculator?
I guess there are some simple principles in number theory in cryptography to calculate such things.
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 numbera1
so that0 <= a1 < m
anda = a1 mod m
. Then repeatedly in a loop multiply bya1
and reduce againmod m
. Thus, in pseudocode:By doing this, we avoid numbers larger than
m^2
. This is the key. The reason we avoid numbers larger thanm^2
is because at every step0 <= p < m
and0 <= a1 < m
.As an example, let's compute
5^55 mod 221
. First,5
is already reducedmod 221
.1 * 5 = 5 mod 221
5 * 5 = 25 mod 221
25 * 5 = 125 mod 221
125 * 5 = 183 mod 221
183 * 5 = 31 mod 221
31 * 5 = 155 mod 221
155 * 5 = 112 mod 221
112 * 5 = 118 mod 221
118 * 5 = 148 mod 221
148 * 5 = 77 mod 221
77 * 5 = 164 mod 221
164 * 5 = 157 mod 221
157 * 5 = 122 mod 221
122 * 5 = 168 mod 221
168 * 5 = 177 mod 221
177 * 5 = 1 mod 221
1 * 5 = 5 mod 221
5 * 5 = 25 mod 221
25 * 5 = 125 mod 221
125 * 5 = 183 mod 221
183 * 5 = 31 mod 221
31 * 5 = 155 mod 221
155 * 5 = 112 mod 221
112 * 5 = 118 mod 221
118 * 5 = 148 mod 221
148 * 5 = 77 mod 221
77 * 5 = 164 mod 221
164 * 5 = 157 mod 221
157 * 5 = 122 mod 221
122 * 5 = 168 mod 221
168 * 5 = 177 mod 221
177 * 5 = 1 mod 221
1 * 5 = 5 mod 221
5 * 5 = 25 mod 221
25 * 5 = 125 mod 221
125 * 5 = 183 mod 221
183 * 5 = 31 mod 221
31 * 5 = 155 mod 221
155 * 5 = 112 mod 221
112 * 5 = 118 mod 221
118 * 5 = 148 mod 221
148 * 5 = 77 mod 221
77 * 5 = 164 mod 221
164 * 5 = 157 mod 221
157 * 5 = 122 mod 221
122 * 5 = 168 mod 221
168 * 5 = 177 mod 221
177 * 5 = 1 mod 221
1 * 5 = 5 mod 221
5 * 5 = 25 mod 221
25 * 5 = 125 mod 221
125 * 5 = 183 mod 221
183 * 5 = 31 mod 221
31 * 5 = 155 mod 221
155 * 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 ofb
. Note that with the algorithm that I described above, the exponentiation by squaring improvement, you end up with the right-to-left binary method.Thus, since 55 = 110111 in binary
1 * (5^1 mod 221) = 5 mod 221
5 * (5^2 mod 221) = 125 mod 221
125 * (5^4 mod 221) = 112 mod 221
112 * (5^16 mod 221) = 112 mod 221
112 * (5^32 mod 221) = 112 mod 221
Therefore the answer is
5^55 = 112 mod 221
. The reason this works is becauseso that
In the step where we calculate
5^1 mod 221
,5^2 mod 221
, etc. we note that5^(2^k)
=5^(2^(k-1)) * 5^(2^(k-1))
because2^k = 2^(k-1) + 2^(k-1)
so that we can first compute5^1
and reducemod 221
, then square this and reducemod 221
to obtain5^2 mod 221
, etc.The above algorithm formalizes this idea.
Just provide another implementation of Jason's answer by C.
After discussing with my classmates, based on Jason's explanation, I like the recursive version more if you don't care about the performance very much:
For example:
Jason's answer in Java (note
i < exp
).Chinese Remainder Theorem comes to mind as an initial point as 221 = 13 * 17. So, break this down into 2 parts that get combined in the end, one for mod 13 and one for mod 17. Second, I believe there is some proof of a^(p-1) = 1 mod p for all non zero a which also helps reduce your problem as 5^55 becomes 5^3 for the mod 13 case as 13*4=52. If you look under the subject of "Finite Fields" you may find some good results on how to solve this.
EDIT: The reason I mention the factors is that this creates a way to factor zero into non-zero elements as if you tried something like 13^2 * 17^4 mod 221, the answer is zero since 13*17=221. A lot of large numbers aren't going to be prime, though there are ways to find large primes as they are used a lot in cryptography and other areas within Mathematics.
This is part of code I made for IBAN validation. Feel free to use.
This is called modular exponentiation(https://en.wikipedia.org/wiki/Modular_exponentiation).
Let's assume you have the following expression:
Instead of powering 19 directly you can do the following:
But this can take also a long time due to a lot of sequential multipliations and so you can multiply on squared values:
Modular exponentiation algorithm makes assumptions that:
And so recursive modular exponentiation algorithm will look like this in java:
Special thanks to @chux for found mistake with incorrect return value in case of y and 0 comparison.