Here is another example, m = 17, 4 x 13 = 52 = 17 x 3 + 1, so the remainder when 52 is divided by 17 is 1, and thus 13 is the inverse of 4 modulo 17. You are to write a method modularInverse() which accepts as input the two integers x and m,
In java pls! Thank you!
In many cryptographic applications the Modular Inverse is a key operation. This
Given 0 < x < m, where x and m are integers, the modular inverse of x is the unique integer n, 0 < n < m, such that the remainder upon dividing x × n by m is 1. That is, x × n mod m = 1.
Modular inverse is a concept that extends the arithmetic inverse. For example, the inverse of 2 is ½ because 2 x ½ = 1 with decimal operation. Here the inverse number can be a fraction.
In modular inverse, we only allow integers, no fraction. So not all integer has an inverse.
For example, m = 26, x = 3, the modular inverse of 3 is 9 because 3 x 9 mod 26 = 1.
However, m = 26, x = 2, the modular inverse of 2 does not exist because you cannot find a number y so that 2 x y mod 26 = 1.
Here is another example, m = 17, 4 x 13 = 52 = 17 x 3 + 1, so the remainder when 52 is divided by 17 is 1, and thus 13 is the inverse of 4 modulo 17.
You are to write a method modularInverse() which accepts as input the two integers x and m, and outputs either the modular inverse n, or -1 to indicate "No such integer exists." if there is no such integer n.
You may assume that m ≤ 100, x and m non-negative.
public class ModInverse {
public static void main(String[] args) {
int m = 26;
for (int x = 0; x < m; x++) {
int n = modularInverse(x, m);
if (n == -1) {
System.out.println("The modular inverse of x " + x + " does not exit.");
} else {
System.out.println("The modular inverse of x " + x + " is: " + n + "for mod " + m);
}
}
//Expect: the mod inverse with mod 26 as follows
// 1 3 5 7 9 11 15 17 19 21 23 25
// 1 9 21 15 3 19 7 23 11 5 17 25
}
public static int modularInverse(int x, int m) {
//your code
return -1;
}
}

Step by step
Solved in 3 steps with 1 images









