Modular arithmetic is a system of arithmetic for numbers where numbers "wrap around" after reaching a certain value, called the modulus. It mainly uses remainders to get the value after wrap around. It is often referred to as "clock arithmetic. As you can see, the time values wrap after reaching 12 and 9 + 4 = 13 is computed as remainder of 13 when divided by 12. The concept is widely used in various fields, including cryptography, computer science, and engineering.
When dividing two integers, we get an equation like this:
\dfrac{A}{B} = Q \text{ remainder } R
Where:
- A is the dividend
- Q is the quotient
- B is the divisor
- R is the remainder
The modulo operator (mod) helps us focus on the remainder:
A mod B=R
Example:
\frac{13}{5}=2 remainder 3
13mod 5=3
So, dividing 13 by 5 gives a remainder of 3
What is Modular Arithmetic?
In modular arithmetic, numbers are reduced within a certain range, defined by the modulus. For two integers a and b, and a positive integer n, we say that a is congruent to b modulo n if their difference is an integer multiple of n. This is denoted as:
a ≡ b (mod n)
Quotient Remainder Theorem
Quotient Remainder Theorem states that for any pair of integers a and b (b is positive), there exist 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, meaning:
20 = 6 x 3 + 2
Modular Operations
Modular Addition
The 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
The same rule is to modular subtraction. We don't require much modular subtraction but it can also be done in the same way.
Modular Multiplication
The 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
The Modular Divisionis 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 the 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 the inverse of a under modulo m, if (a x b) mod m = 1 then b is the 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
There is often a need to efficiently calculate the value of xn mod m. This can be done in O(logn) time using the following recursion:

It is important that in the case of an even n, the value of xn/2 is calculated only once.
This guarantees that the time complexity of the algorithm is O(logn) because n is always halved when it is even.
C++
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
//function that calculate modular exponentiation x^n mod m.
int modpower(int x, int n, int m)
{
if (n == 0) //base case
return 1%m;
long long u = modpower(x,n/2,m);
u = (u*u)%m;
if (n%2 == 1) //when 'n' is odd
u = (u*x)%m;
return u;
}
//driver function
int main()
{
cout<<modpower(5,2,7)<<endl;
return 0;
}
C
#include <stdio.h>
//function that calculate modular exponentiation x^n mod m.
int modpower(int x, int n, int m)
{
if (n == 0) //base case
return 1 % m;
long long u = modpower(x, n / 2, m);
u = (u * u) % m;
if (n % 2 == 1) // when 'n' is odd
u = (u * x) % m;
return u;
}
//driver function
int main()
{
printf("%d\n", modpower(5, 2, 7));
return 0;
}
Java
import java.util.*;
class GFG {
//function that calculate modular exponentiation x^n mod m.
public static int modpower(int x, int n, int m) {
if (n == 0) //base case
return 1 % m;
long u = modpower(x, n / 2, m);
u = (u * u) % m;
if (n % 2 == 1) // when 'n' is odd
u = (u * x) % m;
return (int)u;
}
//driver function
public static void main(String[] args) {
System.out.println(modpower(5, 2, 7));
}
}
Python
#function that calculate modular exponentiation x^n mod m.
def modpower(x, n, m):
if n == 0: # base case
return 1 % m
u = modpower(x, n // 2, m)
u = (u * u) % m
if n % 2 == 1: # when 'n' is odd
u = (u * x) % m
return u
#driver function
print(modpower(5, 2, 7))
Javascript
// function that calculates modular exponentiation x^n mod m.
function modpower(x, n, m) {
if (n == 0) { // base case
return 1 % m;
}
let u = modpower(x, Math.floor(n / 2), m);
u = (u * u) % m;
if (n % 2 == 1) { // when 'n' is odd
u = (u * x) % m;
}
return u;
}
// driver function
console.log(modpower(5, 2, 7));
output:
4
Time complexity: O(logn), because n is always halved when it is even.
Fermat’s theorem states that
xm−1 mod m = 1
when m is prime and x and m are coprime. This also yields
xk mod m = xk mod (m−1) mod m.
Applications of Modular Arithmetic
1. Cryptography: Modular arithmetic is fundamental in cryptography, particularly in public-key cryptosystems like RSA, which relies on the difficulty of factoring large numbers and properties of modular exponentiation.
2. Computer Science: Modular arithmetic is used in hashing algorithms, checksums, and cryptographic hash functions to ensure data integrity and security.
3. Number Theory: In number theory, modular arithmetic helps solve congruences and Diophantine equations, contributing to the understanding of integer properties and relationships.
4. Digital Signal Processing: Modular arithmetic is used in algorithms for efficient computation in digital signal processing, particularly in the Fast Fourier Transform (FFT) and error-correcting codes.
5. Clock Arithmetic: The concept of modular arithmetic is akin to how clocks work, where the hours wrap around after reaching 12 or 24.
Solved Examples of Modular Arithmetic
Example 1: Problem: Show that 38 ≡ 14 (mod 12)
Solution:
38 = 3 × 12 + 2
14 = 1 × 12 + 2
Both 38 and 14 have the same remainder (2) when divided by 12.
Therefore, 38 ≡ 14 (mod 12)
Example 2: Addition in Modular Arithmetic
Problem: Compute (27 + 19) mod 7
Solution:
27 ≡ 6 (mod 7) because 27 = 3 × 7 + 6
19 ≡ 5 (mod 7) because 19 = 2 × 7 + 5
(27 + 19) mod 7 ≡ (6 + 5) mod 7 ≡ 11 mod 7 ≡ 4
Example 3: Multiplication in Modular Arithmetic
Problem: Compute (23 × 17) mod 5
Solution:
23 ≡ 3 (mod 5) because 23 = 4 × 5 + 3
17 ≡ 2 (mod 5) because 17 = 3 × 5 + 2
(23 × 17) mod 5 ≡ (3 × 2) mod 5 ≡ 6 mod 5 ≡ 1
Example 4: Modular Exponentiation
Problem: Compute 7^100 mod 11
Solution:
Use Euler's theorem: a^φ(n) ≡ 1 (mod n) for coprime a and n
φ(11) = 10 (Euler's totient function for prime 11)
7^100 ≡ 7^(10 × 10) ≡ (7^10)^10 ≡ 1^10 ≡ 1 (mod 11)
Example 5: Linear Congruence
Problem: Solve 5x ≡ 3 (mod 7)
Solution:
Multiply both sides by 3 (modular multiplicative inverse of 5 mod 7):
3 × 5x ≡ 3 × 3 (mod 7)
15x ≡ 9 (mod 7)
x ≡ 2 (mod 7)
Example 6: Chinese Remainder Theorem
Problem: Solve the system of congruences:
x ≡ 2 (mod 3)
x ≡ 3 (mod 5)
x ≡ 2 (mod 7)
Solution:
M = 3 × 5 × 7 = 105
M1 = 105/3 = 35, y1 = 35^(-1) mod 3 = 2
M2 = 105/5 = 21, y2 = 21^(-1) mod 5 = 1
M3 = 105/7 = 15, y3 = 15^(-1) mod 7 = 1
x = (2 × 35 × 2 + 3 × 21 × 1 + 2 × 15 × 1) mod 105
= (140 + 63 + 30) mod 105
= 233 mod 105
= 23
Verify: 23 ≡ 2 (mod 3), 23 ≡ 3 (mod 5), 23 ≡ 2 (mod 7)
Example 7: Modular Inverse
Problem: Find the modular inverse of 3 modulo 11
Solution:
Use extended Euclidean algorithm:
11 = 3 × 3 + 2
3 = 1 × 2 + 1
2 = 2 × 1 + 0
Working backwards:
1 = 3 - 1 × 2
1 = 3 - 1 × (11 - 3 × 3)
1 = 4 × 3 - 1 × 11
Therefore, 3^(-1) ≡ 4 (mod 11)
Verify: (3 × 4) mod 11 = 12 mod 11 = 1
Example 8: Fermat's Little Theorem
Problem: Calculate 7^222 mod 11
Solution:
Fermat's Little Theorem states that if p is prime and a is not divisible by p, then:
a^(p-1) ≡ 1 (mod p)
Here, p = 11 (prime) and a = 7 (not divisible by 11)
So, 7^10 ≡ 1 (mod 11)
Now, 222 = 22 × 10 + 2
Therefore, 7^222 ≡ (7^10)^22 × 7^2 (mod 11)
≡ 1^22 × 7^2 (mod 11)
≡ 49 (mod 11)
≡ 5 (mod 11)
Practice Problems on Modular Arithmetic
1. Calculate 47 mod 7.
2. Solve the linear congruence: 5x ≡ 3 (mod 8)
3. Find the modular multiplicative inverse of 5 modulo 11.
4. Using Fermat's Little Theorem, calculate 7100 mod 11.
5. Solve the system of congruences: x ≡ 2 (mod 3)
x ≡ 3 (mod 5)
x ≡ 4 (mod 7)
6. Determine whether 29 is prime using Wilson's Theorem.
7. Calculate (17 × 23 + 31) mod 13.
8. Find the remainder when 3200 is divided by 7.
9. Solve the congruence: x2 ≡ 4 (mod 11)
10. Given that 710 ≡ 1 (mod 11), find 7103 mod 11 without direct computation.
Related Articles:
Similar Reads
Engineering Mathematics Tutorials Engineering mathematics is a vital component of the engineering discipline, offering the analytical tools and techniques necessary for solving complex problems across various fields. Whether you're designing a bridge, optimizing a manufacturing process, or developing algorithms for computer systems,
3 min read
Linear Algebra
MatricesMatrices are key concepts in mathematics, widely used in solving equations and problems in fields like physics and computer science. A matrix is simply a grid of numbers, and a determinant is a value calculated from a square matrix.Example: \begin{bmatrix} 6 & 9 \\ 5 & -4 \\ \end{bmatrix}_{2
3 min read
Row Echelon FormRow Echelon Form (REF) of a matrix simplifies solving systems of linear equations, understanding linear transformations, and working with matrix equations. A matrix is in Row Echelon form if it has the following properties:Zero Rows at the Bottom: If there are any rows that are completely filled wit
4 min read
Eigenvalues and EigenvectorsEigenvalues and eigenvectors are fundamental concepts in linear algebra, used in various applications such as matrix diagonalization, stability analysis and data analysis (e.g., PCA). They are associated with a square matrix and provide insights into its properties.Eigen value and Eigen vectorTable
10 min read
System of Linear EquationsA system of linear equations is a set of two or more linear equations involving the same variables. Each equation represents a straight line or a plane and the solution to the system is the set of values for the variables that satisfy all equations simultaneously.Here is simple example of system of
5 min read
Matrix DiagonalizationMatrix diagonalization is the process of reducing a square matrix into its diagonal form using a similarity transformation. This process is useful because diagonal matrices are easier to work with, especially when raising them to integer powers.Not all matrices are diagonalizable. A matrix is diagon
8 min read
LU DecompositionLU decomposition or factorization of a matrix is the factorization of a given square matrix into two triangular matrices, one upper triangular matrix and one lower triangular matrix, such that the product of these two matrices gives the original matrix. It is a fundamental technique in linear algebr
6 min read
Finding Inverse of a Square Matrix using Cayley Hamilton Theorem in MATLABMatrix is the set of numbers arranged in rows & columns in order to form a Rectangular array. Here, those numbers are called the entries or elements of that matrix. A Rectangular array of (m*n) numbers in the form of 'm' horizontal lines (rows) & 'n' vertical lines (called columns), is calle
4 min read
Sequence & Series
Calculus
Limits, Continuity and DifferentiabilityLimits, Continuity, and Differentiation are fundamental concepts in calculus. They are essential for analyzing and understanding function behavior and are crucial for solving real-world problems in physics, engineering, and economics.Table of ContentLimitsKey Characteristics of LimitsExample of Limi
10 min read
Cauchy's Mean Value TheoremCauchy's Mean Value theorem provides a relation between the change of two functions over a fixed interval with their derivative. It is a special case of Lagrange Mean Value Theorem. Cauchy's Mean Value theorem is also called the Extended Mean Value Theorem or the Second Mean Value Theorem.According
7 min read
Taylor SeriesA Taylor series represents a function as an infinite sum of terms, calculated from the values of its derivatives at a single point.Taylor series is a powerful mathematical tool used to approximate complex functions with an infinite sum of terms derived from the function's derivatives at a single poi
8 min read
Inverse functions and composition of functionsInverse Functions - In mathematics a function, a, is said to be an inverse of another, b, if given the output of b a returns the input value given to b. Additionally, this must hold true for every element in the domain co-domain(range) of b. In other words, assuming x and y are constants, if b(x) =
3 min read
Definite Integral | Definition, Formula & How to CalculateA definite integral is an integral that calculates a fixed value for the area under a curve between two specified limits. The resulting value represents the sum of all infinitesimal quantities within these boundaries. i.e. if we integrate any function within a fixed interval it is called a Definite
8 min read
Application of Derivative - Maxima and MinimaDerivatives have many applications, like finding rate of change, approximation, maxima/minima and tangent. In this section, we focus on their use in finding maxima and minima.Note: If f(x) is a continuous function, then for every continuous function on a closed interval has a maximum and a minimum v
6 min read
Probability & Statistics
Mean, Variance and Standard DeviationMean, Variance and Standard Deviation are fundamental concepts in statistics and engineering mathematics, essential for analyzing and interpreting data. These measures provide insights into data's central tendency, dispersion, and spread, which are crucial for making informed decisions in various en
10 min read
Conditional ProbabilityConditional probability defines the probability of an event occurring based on a given condition or prior knowledge of another event. It is the likelihood of an event occurring, given that another event has already occurred. In probability, this is denoted as A given B, expressed as P(A | B), indica
12 min read
Bayes' TheoremBayes' Theorem is a mathematical formula used to determine the conditional probability of an event based on prior knowledge and new evidence. It adjusts probabilities when new information comes in and helps make better decisions in uncertain situations.Bayes' Theorem helps us update probabilities ba
13 min read
Probability Distribution - Function, Formula, TableA probability distribution is a mathematical function or rule that describes how the probabilities of different outcomes are assigned to the possible values of a random variable. It provides a way of modeling the likelihood of each outcome in a random experiment.While a Frequency Distribution shows
13 min read
Covariance and CorrelationCovariance and correlation are the two key concepts in Statistics that help us analyze the relationship between two variables. Covariance measures how two variables change together, indicating whether they move in the same or opposite directions. Relationship between Independent and dependent variab
6 min read
Practice Questions