Search code examples
javamodular

Using Java to compute 11^-1 mod 26


How would I go about this n Java?

double power = (Math.pow(11,-1)) % 26;
System.out.println(power);

Just returns 0.09090909090909091. According to wolfram

Thanks!


Solution

  • Java is technically correct, the inverse of 11 mod 26 is (approximately) 0.09090909090909 because 0.09090909090909 * 11 is approximately 1, whether mod 26 or not.

    However, what you're trying to find is an integer with the same property, 19, because 19*11 = 1 mod 26, and you can't do that with the same approach.

    Fortunately, the standard library has you covered:

    import java.math.BigInteger;
    
    class Test {
        public static void main(String[] args) {
            System.out.println(
                new BigInteger("11").modInverse(new BigInteger("26")));
        }
    }