Modular Arithmetic
Modular arithmetic is the branch of arithmetic mathematics related with the “mod” functionality. Basically, modular arithmetic is related with computation of “mod” of expressions. Expressions may have digits and computational symbols of addition, subtraction, multiplication, division or any other. Here we will discuss briefly about all modular arithmetic operations.
Quotient Remainder Theorem :
It states that, for any pair of integers a and b (b is positive), there exists two unique integers q and r such that:
a = b x q + r
where 0 <= r < b
Example:
If a = 20, b = 6
then q = 3, r = 2
20 = 6 x 3 + 2
Modular Addition :
Rule for modular addition is:
(a + b) mod m = ((a mod m) + (b mod m)) mod m
Example:
(15 + 17) % 7 = ((15 % 7) + (17 % 7)) % 7 = (1 + 3) % 7 = 4 % 7 = 4
Same rule is for modular subtraction. We don’t require much modular subtraction but it can also be done in same way.
Modular Multiplication :
Rule for modular multiplication is:
(a x b) mod m = ((a mod m) x (b mod m)) mod m
Example:
(12 x 13) % 5 = ((12 % 5) x (13 % 5)) % 5 = (2 x 3) % 5 = 6 % 5 = 1
Modular Division :
Modular division is totally different from modular addition, subtraction and multiplication. It also does not exist always.
(a / b) mod m is not equal to ((a mod m) / (b mod m)) mod m.
This is calculated using following formula:
(a / b) mod m = (a x (inverse of b if exists)) mod m
Modular Inverse :
The modular inverse of a mod m exists only if a and m are relatively prime i.e. gcd(a, m) = 1.
Hence, for finding inverse of a under modulo m,
if (a x b) mod m = 1 then b is modular inverse of a.
Example:
a = 5, m = 7
(5 x 3) % 7 = 1
hence, 3 is modulo inverse of 5 under 7.
Modular Exponentiation :
Finding a^b mod m is the modular exponentiation. There are two approaches for this – recursive and iterative.
Example:
a = 5, b = 2, m = 7 (5 ^ 2) % 7 = 25 % 7 = 4
Below are some more important concepts related to Modular Arithmetic
- Euler’s Totient Function
- Compute n! under modulo p
- Wilson’s Theorem
- How to compute mod of a big number?
- Find value of y mod (2 raised to power x)
Recent Articles on Modular Arithmetic.
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 Division
- Modular Addition
- Modular Multiplication
- Trick for modular division ( (x1 * x2 .... xn) / b ) mod (m)
- Modular multiplicative inverse from 1 to n
- Modular exponentiation (Recursive)
- Modular multiplicative inverse
- How to avoid overflow in modular multiplication?
- Modular Exponentiation of Complex Numbers
- Number of solutions to Modular Equations
- Find modular node in a linked list
- Using Chinese Remainder Theorem to Combine Modular equations
- Proof of De-Morgan's laws in boolean algebra
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.

