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
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
Recommended Posts:
- Modular Exponentiation (Power in Modular Arithmetic)
- Modular Arithmetic
- Modular Division
- Modular Multiplication
- Trick for modular division ( (x1 * x2 .... xn) / b ) mod (m)
- Modular multiplicative inverse
- Modular multiplicative inverse from 1 to n
- Modular exponentiation (Recursive)
- Number of solutions to Modular Equations
- Modular Exponentiation of Complex Numbers
- How to avoid overflow in modular multiplication?
- Find modular node in a linked list
- Using Chinese Remainder Theorem to Combine Modular equations
- Turing Machine for addition
- Overflow in Arithmetic Addition in Binary Number System
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.

