Modular Multiplication

Below are some interesting properties of Modular Multiplication

(a x b) mod m = ((a mod m) x (b mod m)) mod m

(a x b x c) mod m = ((a mod m) x (b mod m) x (c mod m)) mod m

The same property holds for more than three numbers.

The above formula is the extended version of the following formula:



Example 1:
Find the remainder of 15 x 17 x 19 when divided by 7.
Solution:
On dividing 15 by 7 we get 1 as remainder.
On dividing 17 by 7 we get 3 as remainder.
On dividing 19 by 7 we get 5 as remainder.
Remainder of the expression (15 x 17 x 19)/7 will be equal to (1 x 3 x 5)/7.
Combined remainder will be equal to remainder of 15/7 i.e. 1.

Example 2:
Find the remainder of 1421 x 1423 x 1425 when divided by 12.
Solution:
On dividing 1421 by 12 we get 5 as remainder.
On dividing 1423 by 12 we get 7 as remainder.
On dividing 1425 by 12 we get 9 as remainder.
Rem [(1421 x 1423 x 1425)/12] = Rem [(5 x 7 x 9)/12]
Rem [(35 x 9)/12] = Rem [(11 x 9)/12]
Rem [99/12] = 3.

How is it useful?
If we need to find remainder of multiplication of two large numbers, we can avoid doing the multiplication of large numbers, especially helpful in programming where multiplication of large numbers can cause overflow.

Proof: If we prove for two numbers, then we can easily generalize it. Let us see proof for two numbers.

Let a % m = r1
and b % m = r2

Then a and b can be written as (using quotient
theorem).  Here q1 is quotient when we divide 
a by m and r1 is remainder. Similar meanings 
are there for q2 and r2
a = m x q1 + r1
b = m x q2 + r2

LHS = (a x b) % m

    = ((m x q1 + r1 ) x (m x q2 + r2) ) % m

    = (m x m x q1 x q2 + 
        m x q1 x r2 + 
        m x q2 x r1 + r1 x r2 ) % m

    = (m x (m x q1 x q2 + q1 x r2 + 
        q2 x r1) + 
        r1 x r2 ) % m

We can eliminate the multiples of m when
we take the mod m.
LHS = (r1 x r2) % m

RHS = (a % m x a % m) % m
    = (r1 x r2) % m

Hence, LHS = RHS

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.