Modular Addition

Below are some interesting properties of Modular Addition:

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

Example 1:
Find the remainder of 22 + 26 + 29 when divided by 5.
Solution:
On dividing 22 by 5 we get 2 as remainder.
On dividing 26 by 5 we get 1 as remainder.
On dividing 29 by 5 we get 4 as remainder.
Remainder of the expression (22 + 26 + 29)/5 will be equal to (2 + 1 + 4)/5.
Combined remainder will be equal to remainder of 7/5 i.e. 2.

Example 2:
Find the remainder of 1234 + 5678 + 1478 when divided by 11.
Solution:
On dividing 1234 by 11 we get 5 as remainder.
On dividing 5678 by 11 we get 7 as remainder.
On dividing 1478 by 11 we get 9 as remainder.
Rem [(1234 + 5678 + 1478)/11] = Rem [(2 + 2 + 4)/11]
Rem [8/11] = 8.

How is it useful?
If we need to find remainder of addition of two large numbers, we can avoid doing the addition of large numbers, especially helpful in programming where addition of large numbers can cause overflow. Like in C, C++ programming languages if addition of two large numbers exceed 10^18 then overflow happens and we start getting unexpected results.

Proof: Here we will prove for two numbers and thus it can be generalized for more than 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 + b) % m

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

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

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

RHS = (a % m + b % m) % m

    = (r1 + 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.