Java Noob
Java Noob

Reputation: 67

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!

Upvotes: 1

Views: 1686

Answers (2)

Cedric Reichenbach
Cedric Reichenbach

Reputation: 9319

Wolframalfa doesn't interpret your syntax as expected. Instead of writing

11^-1 mod 26

try

mod(11^-1, 26)

which will return the same result as your Java snippet.

Upvotes: 1

that other guy
that other guy

Reputation: 123490

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")));
    }
}

Upvotes: 7

Related Questions