Posted on by Kalkicode
Code Mathematics

Modular multiplicative inverse

Modular multiplicative inverse is a crucial concept in number theory and cryptography. Given two integers 'a' and 'm', where 'a' is coprime to 'm' (i.e., gcd(a, m) = 1), the modular multiplicative inverse of 'a' modulo 'm' is another integer 'x' such that (a * x) % m = 1. In simpler terms, it's finding the number that, when multiplied by 'a' and then taken modulo 'm', results in 1. This article discusses the process of finding modular multiplicative inverses and presents a C program to compute them.

Problem Statement

Given two integers 'a' and 'm', the problem is to find an integer 'x' such that (a * x) % m = 1.

Example

For example, let's take 'a' = 7 and 'm' = 34. The modular multiplicative inverse of 7 modulo 34 is 5, because (7 * 5) % 34 = 35 % 34 = 1. Similarly, for 'a' = 9 and 'm' = 28, the modular multiplicative inverse is 25, as (9 * 25) % 28 = 225 % 28 = 1. For 'a' = 28 and 'm' = 9, the modular multiplicative inverse is 1, as (28 * 1) % 9 = 28 % 9 = 1.

Idea to Solve

To solve this problem, we need to iterate through all possible values of 'x' until we find the one that satisfies the condition (a * x) % m = 1. Since 'm' can be relatively large, it's not efficient to check all possible values of 'x' linearly. Instead, we can use a loop to iterate through integers and use the property that (a * x) % m = 1 is equivalent to (a * x) ≡ 1 (mod m). This can be solved using the Extended Euclidean Algorithm, which finds 'x' and 'y' such that (a * x) + (m * y) = gcd(a, m) = 1.

Pseudocode

function invert_modulo(a, m):
    num = a % m
    i = 1
    result = -1
    while i < m and result == -1:
        if (num * i) % m == 1:
            result = i
        i++
    if result == -1:
        Print "Inverse not found"
    else:
        Print "Result:", result

function main():
    invert_modulo(7, 34)
    invert_modulo(9, 28)
    invert_modulo(28, 9)

Algorithm Explanation

  1. The invert_modulo function takes two parameters, 'a' and 'm'.
  2. It calculates the remainder of 'a' when divided by 'm' (num = a % m).
  3. It initializes a loop variable 'i' to 1 and a result variable to -1.
  4. The loop iterates from 1 to 'm-1':
    • If (num * i) % m equals 1, it means the modular multiplicative inverse is found (result = i).
  5. If no result is found, it prints "Inverse not found", otherwise, it prints the value of the result.
  6. The main function tests the invert_modulo function with different values.

Code Solution

// C Program
// Modular multiplicative inverse
#include <stdio.h>

// This function find the inverse of a (a^-1) under m
void invert_modulo(int a, int m)
{
	int num = a % m;
	//Loop controlling variable
	int i = 1;
	//result variable
	int result = -1;
	//Display given data information
	printf("\n Number  [a] : %d", a);
	printf("\n Modular [m] : %d", m);
	while (i < m && result == -1)
	{
		if ((num * i) % m == 1)
		{
			//When get result
			result = i;
		}
		i++;
	}
	if (result == -1)
	{
		//When result are not possible
		printf("\n Inverse are not found\n");
	}
	else
	{
		printf("\n Result %d\n", result);
	}
}
int main()
{
	printf("\n Test Modular multiplicative inverse\n");
	//Test function
	invert_modulo(7, 34);
	invert_modulo(9, 28);
	invert_modulo(28, 9);
	return 0;
}

Output

 Test Modular multiplicative inverse

 Number  [a] : 7
 Modular [m] : 34
 Result 5

 Number  [a] : 9
 Modular [m] : 28
 Result 25

 Number  [a] : 28
 Modular [m] : 9
 Result 1
// Java program
// Modular multiplicative inverse
class ModularInverse
{
	// This function find the inverse of a (a^-1) under m
	public void invert_modulo(int a, int m)
	{
		int num = a % m;
		//Loop controlling variable
		int i = 1;
		//result variable
		int result = -1;
		//Display given data information
		System.out.print("\n Number [a] : " + a);
		System.out.print("\n Modular [m] : " + m);
		while (i < m && result == -1)
		{
			if ((num * i) % m == 1)
			{
				//When get result
				result = i;
			}
			i++;
		}
		if (result == -1)
		{
			//When result are not possible
			System.out.print("\n Inverse are not found\n");
		}
		else
		{
			System.out.print("\n Result " + result + "\n");
		}
	}
	public static void main(String[] args)
	{
		ModularInverse obj = new ModularInverse();
		System.out.print("\n Test Modular multiplicative inverse\n");
		// Test Case
		obj.invert_modulo(7, 34);
		obj.invert_modulo(9, 28);
		obj.invert_modulo(28, 9);
	}
}

Output

 Test Modular multiplicative inverse

 Number [a] : 7
 Modular [m] : 34
 Result 5

 Number [a] : 9
 Modular [m] : 28
 Result 25

 Number [a] : 28
 Modular [m] : 9
 Result 1
//Include header file
#include <iostream>

using namespace std;
// C++ program
// Modular multiplicative inverse
class ModularInverse
{
	public:
		// This function find the inverse of a (a^-1) under m
		void invert_modulo(int a, int m)
		{
			int num = a % m;
			//Loop controlling variable
			int i = 1;
			//result variable
			int result = -1;
			//Display given data information
			cout << "\n Number [a] : " << a;
			cout << "\n Modular [m] : " << m;
			while (i < m && result == -1)
			{
				if ((num * i) % m == 1)
				{
					//When get result
					result = i;
				}
				i++;
			}
			if (result == -1)
			{
				//When result are not possible
				cout << "\n Inverse are not found\n";
			}
			else
			{
				cout << "\n Result " << result << "\n";
			}
		}
};
int main()
{
	ModularInverse obj = ModularInverse();
	cout << "\n Test Modular multiplicative inverse\n";
	// Test Case
	obj.invert_modulo(7, 34);
	obj.invert_modulo(9, 28);
	obj.invert_modulo(28, 9);
	return 0;
}

Output

 Test Modular multiplicative inverse

 Number [a] : 7
 Modular [m] : 34
 Result 5

 Number [a] : 9
 Modular [m] : 28
 Result 25

 Number [a] : 28
 Modular [m] : 9
 Result 1
//Include namespace system
using System;

// C# program
// Modular multiplicative inverse

class ModularInverse
{
	// This function find the inverse of a (a^-1) under m
	public void invert_modulo(int a, int m)
	{
		int num = a % m;
		//Loop controlling variable
		int i = 1;
		//result variable
		int result = -1;
		//Display given data information
		Console.Write("\n Number [a] : " + a);
		Console.Write("\n Modular [m] : " + m);
		while (i < m && result == -1)
		{
			if ((num * i) % m == 1)
			{
				//When get result
				result = i;
			}
			i++;
		}
		if (result == -1)
		{
			//When result are not possible
			Console.Write("\n Inverse are not found\n");
		}
		else
		{
			Console.Write("\n Result " + result + "\n");
		}
	}
	public static void Main(String[] args)
	{
		ModularInverse obj = new ModularInverse();
		Console.Write("\n Test Modular multiplicative inverse\n");
		// Test Case
		obj.invert_modulo(7, 34);
		obj.invert_modulo(9, 28);
		obj.invert_modulo(28, 9);
	}
}

Output

 Test Modular multiplicative inverse

 Number [a] : 7
 Modular [m] : 34
 Result 5

 Number [a] : 9
 Modular [m] : 28
 Result 25

 Number [a] : 28
 Modular [m] : 9
 Result 1
<?php
// Php program
// Modular multiplicative inverse
class ModularInverse
{
	// This function find the inverse of a (a^-1) under m
	public	function invert_modulo($a, $m)
	{
		$num = $a % $m;
		//Loop controlling variable
		$i = 1;
		//result variable
		$result = -1;
		//Display given data information
		echo "\n Number [a] : ". $a;
		echo "\n Modular [m] : ". $m;
		while ($i < $m && $result == -1)
		{
			if (($num * $i) % $m == 1)
			{
				//When get result
				$result = $i;
			}
			$i++;
		}
		if ($result == -1)
		{
			//When result are not possible
			echo "\n Inverse are not found\n";
		}
		else
		{
			echo "\n Result ". $result ."\n";
		}
	}
}

function main()
{
	$obj = new ModularInverse();
	echo "\n Test Modular multiplicative inverse\n";
	// Test Case
	$obj->invert_modulo(7, 34);
	$obj->invert_modulo(9, 28);
	$obj->invert_modulo(28, 9);
}
main();

Output

 Test Modular multiplicative inverse

 Number [a] : 7
 Modular [m] : 34
 Result 5

 Number [a] : 9
 Modular [m] : 28
 Result 25

 Number [a] : 28
 Modular [m] : 9
 Result 1
// Node Js program
// Modular multiplicative inverse
class ModularInverse
{
	// This function find the inverse of a (a^-1) under m
	invert_modulo(a, m)
	{
		var num = a % m;
		//Loop controlling variable
		var i = 1;
		//result variable
		var result = -1;
		//Display given data information
		process.stdout.write("\n Number [a] : " + a);
		process.stdout.write("\n Modular [m] : " + m);
		while (i < m && result == -1)
		{
			if ((num * i) % m == 1)
			{
				//When get result
				result = i;
			}
			i++;
		}
		if (result == -1)
		{
			//When result are not possible
			process.stdout.write("\n Inverse are not found\n");
		}
		else
		{
			process.stdout.write("\n Result " + result + "\n");
		}
	}
}

function main()
{
	var obj = new ModularInverse();
	process.stdout.write("\n Test Modular multiplicative inverse\n");
	// Test Case
	obj.invert_modulo(7, 34);
	obj.invert_modulo(9, 28);
	obj.invert_modulo(28, 9);
}
main();

Output

 Test Modular multiplicative inverse

 Number [a] : 7
 Modular [m] : 34
 Result 5

 Number [a] : 9
 Modular [m] : 28
 Result 25

 Number [a] : 28
 Modular [m] : 9
 Result 1
#  Python 3 program
#  Modular multiplicative inverse
class ModularInverse :
	#  This function find the inverse of a (a^-1) under m
	def invert_modulo(self, a, m) :
		num = a % m
		# Loop controlling variable
		i = 1
		# result variable
		result = -1
		# Display given data information
		print("\n Number [a] : ", a, end = "")
		print("\n Modular [m] : ", m, end = "")
		while (i < m and result == -1) :
			if ((num * i) % m == 1) :
				# When get result
				result = i
			
			i += 1
		
		if (result == -1) :
			# When result are not possible
			print("\n Inverse are not found\n", end = "")
		else :
			print("\n Result ", result ,"\n", end = "")
		
	

def main() :
	obj = ModularInverse()
	print("\n Test Modular multiplicative inverse\n", end = "")
	#  Test Case
	obj.invert_modulo(7, 34)
	obj.invert_modulo(9, 28)
	obj.invert_modulo(28, 9)

if __name__ == "__main__": main()

Output

 Test Modular multiplicative inverse

 Number [a] :  7
 Modular [m] :  34
 Result  5

 Number [a] :  9
 Modular [m] :  28
 Result  25

 Number [a] :  28
 Modular [m] :  9
 Result  1
#  Ruby program
#  Modular multiplicative inverse
class ModularInverse

	#  This function find the inverse of a (a^-1) under m
	def invert_modulo(a, m)
	
		num = a % m
		# Loop controlling variable
		i = 1
		# result variable
		result = -1
		# Display given data information
		print("\n Number [a] : ", a)
		print("\n Modular [m] : ", m)
		while (i < m && result == -1)
		
			if ((num * i) % m == 1)
			
				# When get result
				result = i
			end
			i += 1
		end
		if (result == -1)
		
			# When result are not possible
			print("\n Inverse are not found\n")
		else
		
			print("\n Result ", result ,"\n")
		end
	end
end
def main()

	obj = ModularInverse.new()
	print("\n Test Modular multiplicative inverse\n")
	#  Test Case
	obj.invert_modulo(7, 34)
	obj.invert_modulo(9, 28)
	obj.invert_modulo(28, 9)
end
main()

Output

 Test Modular multiplicative inverse

 Number [a] : 7
 Modular [m] : 34
 Result 5

 Number [a] : 9
 Modular [m] : 28
 Result 25

 Number [a] : 28
 Modular [m] : 9
 Result 1
// Scala program
// Modular multiplicative inverse
class ModularInverse
{
	// This function find the inverse of a (a^-1) under m
	def invert_modulo(a: Int, m: Int): Unit = {
		var num: Int = a % m;
		//Loop controlling variable
		var i: Int = 1;
		//result variable
		var result: Int = -1;
		//Display given data information
		print("\n Number [a] : " + a);
		print("\n Modular [m] : " + m);
		while (i < m && result == -1)
		{
			if ((num * i) % m == 1)
			{
				//When get result
				result = i;
			}
			i += 1;
		}
		if (result == -1)
		{
			//When result are not possible
			print("\n Inverse are not found\n");
		}
		else
		{
			print("\n Result " + result + "\n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: ModularInverse = new ModularInverse();
		print("\n Test Modular multiplicative inverse\n");
		// Test Case
		obj.invert_modulo(7, 34);
		obj.invert_modulo(9, 28);
		obj.invert_modulo(28, 9);
	}
}

Output

 Test Modular multiplicative inverse

 Number [a] : 7
 Modular [m] : 34
 Result 5

 Number [a] : 9
 Modular [m] : 28
 Result 25

 Number [a] : 28
 Modular [m] : 9
 Result 1
// Swift program
// Modular multiplicative inverse
class ModularInverse
{
	// This function find the inverse of a (a^-1) under m
	func invert_modulo(_ a: Int, _ m: Int)
	{
		let num: Int = a % m;
		//Loop controlling variable
		var i: Int = 1;
		//result variable
		var result: Int = -1;
		//Display given data information
		print("\n Number [a] : ", a, terminator: "");
		print("\n Modular [m] : ", m, terminator: "");
		while (i < m && result == -1)
		{
			if ((num * i) % m == 1)
			{
				//When get result
				result = i;
			}
			i += 1;
		}
		if (result == -1)
		{
			//When result are not possible
			print("\n Inverse are not found\n", terminator: "");
		}
		else
		{
			print("\n Result ", result ,"\n", terminator: "");
		}
	}
}
func main()
{
	let obj: ModularInverse = ModularInverse();
	print("\n Test Modular multiplicative inverse\n", terminator: "");
	// Test Case
	obj.invert_modulo(7, 34);
	obj.invert_modulo(9, 28);
	obj.invert_modulo(28, 9);
}
main();

Output

 Test Modular multiplicative inverse

 Number [a] :  7
 Modular [m] :  34
 Result  5

 Number [a] :  9
 Modular [m] :  28
 Result  25

 Number [a] :  28
 Modular [m] :  9
 Result  1
fn main() 
{
	print!("\n Test Modular multiplicative inverse\n");
	//Test function
	invert_modulo(7, 34);
	invert_modulo(9, 28);
	invert_modulo(28, 9);
	
}
fn invert_modulo(a: i32, m: i32)
{
	let num: i32 = a % m;
	//Loop controlling variable
	let mut i: i32 = 1;
	//result variable
	let mut result: i32 = -1;
	//Display given data information
	print!("\n Number [a] : {}", a);
	print!("\n Modular [m] : {}", m);
	while i < m && result == -1
	{
		if (num * i) % m == 1
		{
			//When get result
			result = i;
		}
		i += 1;
	}
	if result == -1
	{
		//When result are not possible
		print!("\n Inverse are not found\n");
	}
	else
	{
		print!("\n Result {}\n", result);
	}
}

Output

 Test Modular multiplicative inverse

 Number [a] : 7
 Modular [m] : 34
 Result 5

 Number [a] : 9
 Modular [m] : 28
 Result 25

 Number [a] : 28
 Modular [m] : 9
 Result 1

Time Complexity

The time complexity of this code is linear, O(m), where 'm' is the modulus value. The loop iterates up to 'm-1' times, and each iteration involves a constant number of operations. The execution time grows linearly with the modulus value.

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment