(Translated by https://www.hiragana.jp/)
Modular multiplicative inverse - GeeksforGeeks
Open In App

Modular multiplicative inverse

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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 of "A modulo M" exists if and only if A and M are relatively prime (i.e. if gcd(A, M) = 1)

Examples: 

Input: A = 3, M = 11
Output: 4
Explanation: Since (4*3) mod 11 = 1, 4 is modulo inverse of 3(under 11).
One might think, 15 also as a valid output as "(15*3) mod 11" 
is also 1, but 15 is not in range {1, 2, ... 10}, so not valid.

Input:  A = 10, M = 17
Output: 12
Explamation: Since (10*12) mod 17 = 1, 12 is modulo inverse of 10(under 17).

Naive Approach:  To solve the problem, follow the below idea:

A naive method is to try all numbers from 1 to m. For every number x, check if (A * X) % M is 1

Below is the implementation of the above approach:

C++
// C++ program to find modular
// inverse of A under modulo M
#include <bits/stdc++.h>
using namespace std;

// A naive method to find modular
// multiplicative inverse of 'A'
// under modulo 'M'

int modInverse(int A, int M)
{
    for (int X = 1; X < M; X++)
        if (((A % M) * (X % M)) % M == 1)
            return X;
}

// Driver code
int main()
{
    int A = 3, M = 11;

    // Function call
    cout << modInverse(A, M);
    return 0;
}
Java Python C# JavaScript PHP

Output
4

Time Complexity: O(M)
Auxiliary Space: O(1)

Modular multiplicative inverse when M and A are coprime or gcd(A, M)=1:

The idea is to use Extended Euclidean algorithms that take two integers 'a' and 'b', then find their gcd, and also find 'x' and 'y' such that 

ax + by = gcd(a, b)

To find the multiplicative inverse of 'A' under 'M', we put b = M in the above formula. Since we know that A and M are relatively prime, we can put the value of gcd as 1.

Ax + My = 1

If we take modulo M on both sides, we get

Ax + My 1 (mod M)

We can remove the second term on left side as 'My (mod M)' would always be 0 for an integer y. 

Ax   1 (mod M)

So the 'x' that we can find using Extended Euclid Algorithm is the multiplicative inverse of 'A'

Below is the implementation of the above approach:  

C++
// C++ program to find multiplicative modulo
// inverse using Extended Euclid algorithm.
#include <bits/stdc++.h>
using namespace std;

// Function for extended Euclidean Algorithm
int gcdExtended(int a, int b, int* x, int* y);

// Function to find modulo inverse of a
void modInverse(int A, int M)
{
    int x, y;
    int g = gcdExtended(A, M, &x, &y);
    if (g != 1)
        cout << "Inverse doesn't exist";
    else {

        // m is added to handle negative x
        int res = (x % M + M) % M;
        cout << "Modular multiplicative inverse is " << res;
    }
}

// Function for extended Euclidean Algorithm
int gcdExtended(int a, int b, int* x, int* y)
{

    // Base Case
    if (a == 0) {
        *x = 0, *y = 1;
        return b;
    }

    // To store results of recursive call
    int x1, y1;
    int gcd = gcdExtended(b % a, a, &x1, &y1);

    // Update x and y using results of recursive
    // call
    *x = y1 - (b / a) * x1;
    *y = x1;

    return gcd;
}

// Driver Code
int main()
{
    int A = 3, M = 11;

    // Function call
    modInverse(A, M);
    return 0;
}

// This code is contributed by khushboogoyal499
C Java Python C# JavaScript PHP

Output
Modular multiplicative inverse is 4

Time Complexity: O(log M)
Auxiliary Space: O(log M), because of the internal recursion stack.


 Iterative Implementation of the above approach:

C++
// Iterative C++ program to find modular
// inverse using extended Euclid algorithm

#include <bits/stdc++.h>
using namespace std;

// Returns modulo inverse of a with respect
// to m using extended Euclid Algorithm
// Assumption: a and m are coprimes, i.e.,
// gcd(A, M) = 1
int modInverse(int A, int M)
{
    int m0 = M;
    int y = 0, x = 1;

    if (M == 1)
        return 0;

    while (A > 1) {
        // q is quotient
        int q = A / M;
        int t = M;

        // m is remainder now, process same as
        // Euclid's algo
        M = A % M, A = t;
        t = y;

        // Update y and x
        y = x - q * y;
        x = t;
    }

    // Make x positive
    if (x < 0)
        x += m0;

    return x;
}

// Driver Code
int main()
{
    int A = 3, M = 11;

    // Function call
    cout << "Modular multiplicative inverse is "
         << modInverse(A, M);
    return 0;
}
// this code is contributed by shivanisinghss2110
C Java Python C# JavaScript PHP

Output
Modular multiplicative inverse is 4

Time Complexity: O(log m)
Auxiliary Space: O(1)


Modular multiplicative inverse when M is prime:

If we know M is prime, then we can also use Fermat's little theorem to find the inverse. 

aM-1 1 (mod M)

If we multiply both sides with a-1, we get 

a-1 a M-2 (mod M)

Below is the implementation of the above approach:

C++
// C++ program to find modular inverse of A under modulo M
// This program works only if M is prime.
#include <bits/stdc++.h>
using namespace std;

// To find GCD of a and b
int gcd(int a, int b);

// To compute x raised to power y under modulo M
int power(int x, unsigned int y, unsigned int M);

// Function to find modular inverse of a under modulo M
// Assumption: M is prime
void modInverse(int A, int M)
{
    int g = gcd(A, M);
    if (g != 1)
        cout << "Inverse doesn't exist";
    else {
        // If a and m are relatively prime, then modulo
        // inverse is a^(m-2) mode m
        cout << "Modular multiplicative inverse is "
             << power(A, M - 2, M);
    }
}

// To compute x^y under modulo m
int power(int x, unsigned int y, unsigned int M)
{
    if (y == 0)
        return 1;

    int p = power(x, y / 2, M) % M;
    p = (p * p) % M;

    return (y % 2 == 0) ? p : (x * p) % M;
}

// Function to return gcd of a and b
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

// Driver code
int main()
{
    int A = 3, M = 11;

    // Function call
    modInverse(A, M);
    return 0;
}
Java Python C# JavaScript PHP

Output
Modular multiplicative inverse is 4

Time Complexity: O(log M)
Auxiliary Space: O(log M), because of the internal recursion stack.

Applications: 
Computation of the modular multiplicative inverse is an essential step in RSA public-key encryption method.


Similar Reads