VOOZH about

URL: https://www.geeksforgeeks.org/dsa/modular-division/

⇱ Modular Division - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Modular Division

Last Updated : 14 Jul, 2025

Given three positive integers a, b, and M, the objective is to find (a/b) % M i.e., find the value of (a × b-1 ) % M, where b-1 is the modular inverse of b modulo M.

Examples:

Input: a = 10, b = 2, M = 13
Output: 5
Explanation: The modular inverse of 2 modulo 13 is 7, so (10 / 2) % 13 = (10 × 7) % 13 = 70 % 13 = 5.

Input: a = 7, b = 4, M = 9
Output: 4
Explanation: The modular inverse of 4 modulo 9 is 7, so (7 / 4) % 9 = (7 × 7) % 9 = 49 % 9 = 4.

Modular division is the process of dividing one number by another within the rules of modular arithmetic. Unlike regular arithmetic, modular systems do not support direct division. Instead, division is performed by multiplying the dividend by the modular multiplicative inverse of the divisor under a given modulus.

In simple terms, to compute (a/b) % M we instead calculate (a × b-1 ) % M where b-1 is the modular inverse of b modulo M ( i.e., b × b-1 ≡ 1 mod M ).

The following articles are prerequisites for this.

Can we always do modular division?

The answer is "No". First of all, like ordinary arithmetic, division by 0 is not defined. For example, 4/0 is not allowed. In modular arithmetic, not only 4/0 is not allowed, but 4/12 under modulo 6 is also not allowed. The reason is, 12 is congruent to 0 when modulus is 6.

When is modular division defined?

Modular division is defined when the modular inverse of the divisor exists. The inverse of an integer 'x' is another integer 'y' such that (x*y) % m = 1 where m is the modulus. 

When does inverse exist?
As we discussed, inverse of a number 'a' exists under modulo 'm' if 'a' and 'm' are co-prime, i.e., GCD of them is 1.

Modular Arithmetic Rules:

(a * b) % m = ((a % m) * (b % m)) % m
(a + b) % m = ((a % m) + (b % m)) % m
(a - b) % m = ((a % m) - (b % m) + m) % m // m is added to handle negative numbers
But,
(a / b) % m not same as ((a % m)/(b % m)) % m
For example, a = 10, b = 5, m = 5. (a / b) % m is 2, but ((a % m) / (b % m)) % m is not defined.

[Approach 1] Using Fermat's Little Theorem (when M is prime) O(log M) Time and O(1) Space

If the modulus m is a prime number, we can use Fermat’s Little Theorem to find the modular inverse of b. According to the theorem, the inverse of b modulo m is b M-2 % M. So we can use Modular Exponentiation for computing b M-2 % M.


Output
5

[Approach 2] Extended Euclidean Algorithm O(log M) Time and O(1) Space

To find the modular inverse of a number b modulo M using the Extended Euclidean Algorithm, we aim to solve the equation b * x + M * y = gcd(b, M). If the greatest common divisor gcd(b, M) is 1, then x is the modular inverse of b modulo M. The Extended Euclidean Algorithm computes both gcd and the coefficients x and y that satisfy this linear combination. Once x is found, we take its positive equivalent by calculating (x % M + M) % M to get the correct modular inverse. This approach works for any modulus M, not necessarily prime.


Output
5
Comment
Article Tags: