I cannot for the life of me figure out how to find the modular multiplicative inverse mod 5. I have read all the wiki articles, watched videos, and even looked for help from classmates and cannot find a solution to this. Everything I have found is either in another programming language in which I can't translate to Java (new to programming) and/or uses doubles instead of integers, which I can only use integers per my professor's specs. Here is the class I have written so far, can't do the divide()
method until I figure out the inverse()
method:
public class ModInt {
/**
* the integer modulo base
*/
private int base;
/**
* the number
*/
private int number;
/**
* creates the modulo 2 number 0
*/
public ModInt()
{
base = 2;
number = 0;
}
/**
* creates a modulo b number n
* @param n the number
* @param b the base
*/
public ModInt(int n, int b)
{
number = n;
base = b;
}
/**
* creates an equivalent number in the same integer modulo base as the specified integer modulo number.
* @param m an integer modulo number
*/
public ModInt(ModInt m)
{
number = m.number;
base = m.base;
}
/**
* gives the number of the integer modulo number.
* @return the number
*/
public int getNumber()
{
return number;
}
/**
* gives the base of the specified integer modulo number.
* @return the base
*/
public int getBase()
{
return base;
}
/**
* modifies the integer modulo number using the specified parameters
* @param n the new number
* @param b the new base
*/
public void setModInt(int n, int b)
{
number = n;
base = b;
}
/**
* adds this integer modulo number and the specified integer modulo number
* @param m an integer modulo number
* @return the sum of this number and the specified number
*/
public ModInt add(ModInt m)
{
return new ModInt((number + m.number) % base, base);
}
/**
* subtracts this integer modulo number and the specified integer modulo number
* @param m an integer modulo number
* @return the difference this number and the specified number
*/
public ModInt subtract(ModInt m)
{
return new ModInt(((base - number + m.number) % base, base);
}
/**
* multiplies this integer modulo number and the specified integer modulo number
* @param m an integer modulo number
* @return the product of this number and the specified number
*/
public ModInt multiply(ModInt m)
{
return new ModInt((number * m.number) % base, base);
}
/**
* computes the inverse of this integer modulo number
* @return the inverse of this number
*/
public ModInt inverse()
{
return new ModInt();
}
/**
* divides this integer modulo number and the specified integer modulo number
* @param m an integer modulo number
* @return the quotient of this number and the specified number
*/
public ModInt divide(ModInt m)
{
return new ModInt();
}
/**
* give the string representation of an integer modulo number in the format
* n(mod b), where n is the number and b is the base
* @return a string representation of the integer modulo number in the format
* n(mod b); for example 3(mod 5) is the representation of the number
* 3 in integer modulo base 5
*/
public String toString()
{
return String.format("%d(mod %d)", number, base);
}
}
I am trying to write the inverse()
method so it returns the inverse of the integer modulo number (mod 5
). For now, I have it returning just the default constructor so the error would go away when running the code. Can anyone attempt to explain how to find the modular multiplicative inverse using ONLY integer types, no doubles or any other types? Here is my professor's explanation of it, but I don't understand it:
The multiplicative inverse or simply the inverse of a number n, denoted n^(−1), in integer modulo base b, is a number that when multiplied by n is congruent to 1; that is, n × n^(−1) ≡ 1(mod b). For example, 5^(−1) integer modulo 7 is 3 since (5 × 3) mod 7 = 15 mod 7 ≡ 1. The number 0 has no inverse. Not every number is invertible. For example, 2^(−1) integer modulo 4 is indeterminate since no integer in {0, 1, 2, 3} can be multiplied by 2 to obtain 1.
Any help is appreciated, thanks.