Modular multiplicative inverse
Last Updated :
29 Dec, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int modInverse(int A, int M)
{
for (int X = 1; X < M; X++)
if (((A % M) * (X % M)) % M == 1)
return X;
}
int main()
{
int A = 3, M = 11;
cout << modInverse(A, M);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int modInverse(int A, int M)
{
for (int X = 1; X < M; X++)
if (((A % M) * (X % M)) % M == 1)
return X;
return 1;
}
public static void main(String args[])
{
int A = 3, M = 11;
System.out.println(modInverse(A, M));
}
}
|
Python3
def modInverse(A, M):
for X in range(1, M):
if (((A % M) * (X % M)) % M == 1):
return X
return -1
if __name__ == "__main__":
A = 3
M = 11
print(modInverse(A, M))
|
C#
using System;
class GFG {
static int modInverse(int A, int M)
{
for (int X = 1; X < M; X++)
if (((A % M) * (X % M)) % M == 1)
return X;
return 1;
}
public static void Main()
{
int A = 3, M = 11;
Console.WriteLine(modInverse(A, M));
}
}
|
Javascript
<script>
function modInverse(a, m)
{
for(let x = 1; x < m; x++)
if (((a % m) * (x % m)) % m == 1)
return x;
}
let a = 3;
let m = 11;
document.write(modInverse(a, m));
</script>
|
PHP
<?php
function modInverse( $A, $M)
{
for ($X = 1; $X < $M; $X++)
if ((($A%$M) * ($X%$M)) % $M == 1)
return $X;
}
$A = 3;
$M = 11;
echo modInverse($A, $M);
?>
|
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++
#include <bits/stdc++.h>
using namespace std;
int gcdExtended(int a, int b, int* x, int* y);
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 {
int res = (x % M + M) % M;
cout << "Modular multiplicative inverse is " << res;
}
}
int gcdExtended(int a, int b, int* x, int* y)
{
if (a == 0) {
*x = 0, *y = 1;
return b;
}
int x1, y1;
int gcd = gcdExtended(b % a, a, &x1;, &y1;);
*x = y1 - (b / a) * x1;
*y = x1;
return gcd;
}
int main()
{
int A = 3, M = 11;
modInverse(A, M);
return 0;
}
|
C
#include <stdio.h>
int gcdExtended(int a, int b, int* x, int* y);
void modInverse(int A, int M)
{
int x, y;
int g = gcdExtended(A, M, &x;, &y;);
if (g != 1)
printf("Inverse doesn't exist");
else {
int res = (x % M + M) % M;
printf("Modular multiplicative inverse is %d", res);
}
}
int gcdExtended(int a, int b, int* x, int* y)
{
if (a == 0) {
*x = 0, *y = 1;
return b;
}
int x1, y1;
int gcd = gcdExtended(b % a, a, &x1;, &y1;);
*x = y1 - (b / a) * x1;
*y = x1;
return gcd;
}
int main()
{
int A = 3, M = 11;
modInverse(A, M);
return 0;
}
|
Java
public class GFG {
public static int x;
public static int y;
static int gcdExtended(int a, int b)
{
if (a == 0) {
x = 0;
y = 1;
return b;
}
int gcd = gcdExtended(b % a, a);
int x1 = x;
int y1 = y;
int tmp = b / a;
x = y1 - tmp * x1;
y = x1;
return gcd;
}
static void modInverse(int A, int M)
{
int g = gcdExtended(A, M);
if (g != 1) {
System.out.println("Inverse doesn't exist");
}
else {
int res = (x % M + M) % M;
System.out.println(
"Modular multiplicative inverse is " + res);
}
}
public static void main(String[] args)
{
int A = 3, M = 11;
modInverse(A, M);
}
}
|
Python3
x, y = 0, 1
def gcdExtended(a, b):
global x, y
if (a == 0):
x = 0
y = 1
return b
gcd = gcdExtended(b % a, a)
x1 = x
y1 = y
x = y1 - (b // a) * x1
y = x1
return gcd
def modInverse(A, M):
g = gcdExtended(A, M)
if (g != 1):
print("Inverse doesn't exist")
else:
res = (x % M + M) % M
print("Modular multiplicative inverse is ", res)
if __name__ == "__main__":
A = 3
M = 11
modInverse(A, M)
|
C#
using System;
public class GFG {
public static int x, y;
static int gcdExtended(int a, int b)
{
if (a == 0) {
x = 0;
y = 1;
return b;
}
int gcd = gcdExtended(b % a, a);
int x1 = x;
int y1 = y;
x = y1 - (b / a) * x1;
y = x1;
return gcd;
}
static void modInverse(int A, int M)
{
int g = gcdExtended(A, M);
if (g != 1)
Console.Write("Inverse doesn't exist");
else {
int res = (x % M + M) % M;
Console.Write(
"Modular multiplicative inverse is " + res);
}
}
public static void Main(string[] args)
{
int A = 3, M = 11;
modInverse(A, M);
}
}
|
Javascript
<script>
let x, y;
function gcdExtended(a, b){
if (a == 0)
{
x = 0;
y = 1;
return b;
}
let gcd = gcdExtended(b % a, a);
let x1 = x;
let y1 = y;
x = y1 - Math.floor(b / a) * x1;
y = x1;
return gcd;
}
function modInverse(a, m)
{
let g = gcdExtended(a, m);
if (g != 1){
document.write("Inverse doesn't exist");
}
else{
let res = (x % m + m) % m;
document.write("Modular multiplicative inverse is ", res);
}
}
{
let a = 3, m = 11;
modInverse(a, m);
}
</script>
|
PHP
<?php
function modInverse($A, $M)
{
$x = 0;
$y = 0;
$g = gcdExtended($A, $M, $x, $y);
if ($g != 1)
echo "Inverse doesn't exist";
else
{
$res = ($x % $M + $M) % $M;
echo "Modular multiplicative " .
"inverse is " . $res;
}
}
function gcdExtended($a, $b, &$x, &$y)
{
if ($a == 0)
{
$x = 0;
$y = 1;
return $b;
}
$x1;
$y1;
$gcd = gcdExtended($b%$a, $a, $x1, $y1);
$x = $y1 - (int)($b/$a) * $x1;
$y = $x1;
return $gcd;
}
$A = 3;
$M = 11;
modInverse($A, $M);
?>
|
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++
#include <bits/stdc++.h>
using namespace std;
int modInverse(int A, int M)
{
int m0 = M;
int y = 0, x = 1;
if (M == 1)
return 0;
while (A > 1) {
int q = A / M;
int t = M;
M = A % M, A = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0)
x += m0;
return x;
}
int main()
{
int A = 3, M = 11;
cout << "Modular multiplicative inverse is "
<< modInverse(A, M);
return 0;
}
|
C
#include <stdio.h>
int modInverse(int A, int M)
{
int m0 = M;
int y = 0, x = 1;
if (M == 1)
return 0;
while (A > 1) {
int q = A / M;
int t = M;
M = A % M, A = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0)
x += m0;
return x;
}
int main()
{
int A = 3, M = 11;
printf("Modular multiplicative inverse is %d\n",
modInverse(A, M));
return 0;
}
|
Java
class GFG {
static int modInverse(int A, int M)
{
int m0 = M;
int y = 0, x = 1;
if (M == 1)
return 0;
while (A > 1) {
int q = A / M;
int t = M;
M = A % M;
A = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0)
x += m0;
return x;
}
public static void main(String args[])
{
int A = 3, M = 11;
System.out.println("Modular multiplicative "
+ "inverse is "
+ modInverse(A, M));
}
}
|
Python3
def modInverse(A, M):
m0 = M
y = 0
x = 1
if (M == 1):
return 0
while (A > 1):
q = A // M
t = M
M = A % M
A = t
t = y
y = x - q * y
x = t
if (x < 0):
x = x + m0
return x
if __name__ == "__main__":
A = 3
M = 11
print("Modular multiplicative inverse is",
modInverse(A, M))
|
C#
using System;
class GFG {
static int modInverse(int A, int M)
{
int m0 = M;
int y = 0, x = 1;
if (M == 1)
return 0;
while (A > 1) {
int q = A / M;
int t = M;
M = A % M;
A = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0)
x += m0;
return x;
}
public static void Main()
{
int A = 3, M = 11;
Console.WriteLine("Modular multiplicative "
+ "inverse is "
+ modInverse(A, M));
}
}
|
Javascript
<script>
function modInverse(a, m)
{
let m0 = m;
let y = 0;
let x = 1;
if (m == 1)
return 0;
while (a > 1)
{
let q = parseInt(a / m);
let t = m;
m = a % m;
a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0)
x += m0;
return x;
}
let a = 3;
let m = 11;
document.write(`Modular multiplicative inverse is ${modInverse(a, m)}`);
</script>
|
PHP
<?php
function modInverse($A, $M)
{
$m0 = $M;
$y = 0;
$x = 1;
if ($m == 1)
return 0;
while ($A > 1)
{
$q = (int) ($A / $M);
$t = $M;
$M = $A % $M;
$A = $t;
$t = $y;
$y = $x - $q * $y;
$x = $t;
}
if ($x < 0)
$x += $m0;
return $x;
}
$A = 3;
$M = 11;
echo "Modular multiplicative inverse is: ",
modInverse($A, $M);
?>
|
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++
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b);
int power(int x, unsigned int y, unsigned int M);
void modInverse(int A, int M)
{
int g = gcd(A, M);
if (g != 1)
cout << "Inverse doesn't exist";
else {
cout << "Modular multiplicative inverse is "
<< power(A, M - 2, 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;
}
int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
int main()
{
int A = 3, M = 11;
modInverse(A, M);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void modInverse(int A, int M)
{
int g = gcd(A, M);
if (g != 1)
System.out.println("Inverse doesn't exist");
else {
System.out.println(
"Modular multiplicative inverse is "
+ power(A, M - 2, M));
}
}
static int power(int x, int y, int M)
{
if (y == 0)
return 1;
int p = power(x, y / 2, M) % M;
p = (int)((p * (long)p) % M);
if (y % 2 == 0)
return p;
else
return (int)((x * (long)p) % M);
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
public static void main(String args[])
{
int A = 3, M = 11;
modInverse(A, M);
}
}
|
Python3
def modInverse(A, M):
g = gcd(A, M)
if (g != 1):
print("Inverse doesn't exist")
else:
print("Modular multiplicative inverse is ",
power(A, M - 2, M))
def power(x, y, M):
if (y == 0):
return 1
p = power(x, y // 2, M) % M
p = (p * p) % M
if(y % 2 == 0):
return p
else:
return ((x * p) % M)
def gcd(a, b):
if (a == 0):
return b
return gcd(b % a, a)
if __name__ == "__main__":
A = 3
M = 11
modInverse(A, M)
|
C#
using System;
class GFG {
static void modInverse(int A, int M)
{
int g = gcd(A, M);
if (g != 1)
Console.Write("Inverse doesn't exist");
else {
Console.Write(
"Modular multiplicative inverse is "
+ power(A, M - 2, M));
}
}
static int power(int x, int y, int M)
{
if (y == 0)
return 1;
int p = power(x, y / 2, M) % M;
p = (p * p) % M;
if (y % 2 == 0)
return p;
else
return (x * p) % M;
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
public static void Main()
{
int A = 3, M = 11;
modInverse(A, M);
}
}
|
Javascript
<script>
function modInverse(a, m)
{
let g = gcd(a, m);
if (g != 1)
document.write("Inverse doesn't exist");
else
{
document.write("Modular multiplicative inverse is "
+ power(a, m - 2, m));
}
}
function power(x, y, m)
{
if (y == 0)
return 1;
let p = power(x, parseInt(y / 2), m) % m;
p = (p * p) % m;
return (y % 2 == 0) ? p : (x * p) % m;
}
function gcd(a, b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
let a = 3, m = 11;
modInverse(a, m);
</script>
|
PHP
<?php
function modInverse( $A, $M)
{
$g = gcd($A, $M);
if ($g != 1)
echo "Inverse doesn't exist";
else
{
echo "Modular multiplicative inverse is "
, power($A, $M - 2, $M);
}
}
function power( $x, $y, $M)
{
if ($y == 0)
return 1;
$p = power($x, $y / 2, $M) % $M;
$p = ($p * $p) % $M;
return ($y % 2 == 0)? $p : ($x * $p) % $M;
}
function gcd($a, $b)
{
if ($a == 0)
return $b;
return gcd($b % $a, $a);
}
$A = 3;
$M = 11;
modInverse($A, $M);
?>
|
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.
This article is contributed by Ankur.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...