The Wayback Machine - https://web.archive.org/web/20240627104141/https://www.geeksforgeeks.org/modular-multiplication/
Open In App

Modular Multiplication

Last Updated : 19 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 b % m) % m
    = (r1 x r2) % m

Hence, LHS = RHS

 


Previous Article
Next Article

Similar Reads

Modular Exponentiation (Power in Modular Arithmetic)
Given three numbers x, y and p, compute (xy) % p. Examples : Input: x = 2, y = 3, p = 5 Output: 3 Explanation: 2^3 % 5 = 8 % 5 = 3. Input: x = 2, y = 5, p = 13 Output: 6 Explanation: 2^5 % 13 = 32 % 13 = 6.Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. We have discussed recursive and iterative solutions for powe
9 min read
How to avoid overflow in modular multiplication?
Consider below simple method to multiply two numbers. C/C++ Code #include <iostream> using namespace std; #define ll long long // Function to multiply two numbers modulo mod ll multiply(ll a, ll b, ll mod) { // Multiply two numbers and then take modulo to prevent // overflow return ((a % mod) * (b % mod)) % mod; } int main() { ll a = 12345678
6 min read
Number of solutions to Modular Equations
Given A and B, the task is to find the number of possible values that X can take such that the given modular equation (A mod X) = B holds good. Here, X is also called a solution of the modular equation. Examples: Input : A = 26, B = 2Output : 6Explanation:X can be equal to any of {3, 4, 6, 8, 12, 24} as A modulus any of these values equals 2 i. e.,
13 min read
Modular exponentiation (Recursive)
Given three numbers a, b and c, we need to find (ab) % cNow why do “% c” after exponentiation, because ab will be really large even for relatively small values of a, b and that is a problem because the data type of the language that we try to code the problem, will most probably not let us store such a large number.Examples: Input : a = 2312 b = 34
6 min read
Modular Exponentiation of Complex Numbers
Given four integers A, B, K, M. The task is to find (A + iB)K % M which is a complex number too. A + iB represents a complex number. Examples: Input : A = 2, B = 3, K = 4, M = 5 Output: 1 + i*0 Input : A = 7, B = 3, K = 10, M = 97 Output: 25 + i*29 Prerequisite: Modular Exponentiation Approach: An efficient approach is similar to the modular expone
7 min read
Count array elements having modular inverse under given prime number P equal to itself
Given an array arr[] of size N and a prime number P, the task is to count the elements of the array such that modulo multiplicative inverse of the element under modulo P is equal to the element itself. Examples: Input: arr[] = {1, 6, 4, 5}, P = 7Output: 2Explanation:Modular multiplicative inverse of arr[0](=1) under modulo P(= 7) is arr[0](= 1) its
5 min read
XOR of array elements whose modular inverse with a given number exists
Given an array arr[] of length N and a positive integer M, the task is to find the Bitwise XOR of all the array elements whose modular inverse with M exists. Examples: Input: arr[] = {1, 2, 3}, M = 4Output: 2Explanation:Initialize the value xor with 0:For element indexed at 0 i.e., 1, its mod inverse with 4 is 1 because (1 * 1) % 4 = 1 i.e., it exi
7 min read
Modular multiplicative inverse from 1 to n
Give a positive integer n, find modular multiplicative inverse of all integer from 1 to n with respect to a big prime number, say, 'prime'. The modular multiplicative inverse of a is an integer 'x' such that. a x ? 1 (mod prime) Examples: Input : n = 10, prime = 17 Output : 1 9 6 13 7 3 5 15 2 12 Explanation : For 1, modular inverse is 1 as (1 * 1)
9 min read
Modular multiplicative inverse
Given two integers A and M, find the modular multiplicative inverse of A under modulo M.The modular multiplicative inverse is an integer X such that: A X ? 1 (mod M) Note: The value of X should be in the range {1, 2, ... M-1}, i.e., in the range of integer modulo M. ( Note that X cannot be 0 as A*0 mod M will never be 1). The multiplicative inverse
15+ min read
Modular Division
Given three positive numbers a, b and m. Compute a/b under modulo m. The task is basically to find a number c such that (b * c) % m = a % m.Examples: Input : a = 8, b = 4, m = 5 Output : 2 Input : a = 8, b = 3, m = 5 Output : 1 Note that (1*3)%5 is same as 8%5 Input : a = 11, b = 4, m = 5 Output : 4 Note that (4*4)%5 is same as 11%5 Following artic
9 min read
three90RightbarBannerImg