A modular multiplicative inverse of an integer under modulo is an integer such that
and must be coprime, meaning
A common way of finding a modular multiplicative inverse of an integer is to use the extended Euclidean algorithm. To demonstrate this, let’s find a modular multiplicative inverse of 217 under modulo 32.
Pick the larger of the two numbers (217) and, using Euclidean division, divide it by the smaller one (32). Repeat this for the divisor (32) and the remainder obtained (25). Continue until you get a remainder of 1. This gives us
Next, express each remainder as a difference of the divident and a multiple of the divisor. That is, for each equation, subtract the product of the divisor and the quotient from both sides, and flip the equation:
Work backwards to express 1 as a sum of a multiple of the integer (217) and a multiple of the modulus (32):
Transform the last equation to have a multiple of the integer (217) on one side, and a sum of 1 and a multiple of the modulus (32) on the other side. The resulting equation is
which implies
Thus, a modular multiplicative inverse of 217 under modulo 32 is 9.
Of course, any number of the form () is a modular multiplicative inverse of 217 under modulo 32. Generally, the smallest positive modular multiplicative inverse is used. If your result is negative, add the modulus to it as many times as needed to obtain a positive number. If your result is greater than the modulus, subtract the modulus from it until the result is smaller than the modulus.