1

I'm doing some error correcting, and I need to divide two digits under mod 11 in Java.

Now this I know, from using a modular calculator:

9/1 mod 11 = 9
2/10 mod 11 = 9

The problem comes in getting Java to calculate this. In Java:

(9 / 1) % 11 = 9 - This is fine
(2 / 10) % 11 = 0 - This is not correct.

I know that Java cannot technically perform modular operations, and part of me is thinking that I either need to somehow calculate the inverse, or use an array to store the possible output values.

Jeffrey Bosboom
  • 13,313
  • 16
  • 79
  • 92
Tony
  • 3,587
  • 8
  • 44
  • 77
  • 7
    Err, 2 / 10 is 0. And 0 % 11 is 0. Why would it be 9? – JB Nizet Oct 22 '11 at 16:13
  • 3
    Because I'm doing it under mod 11. Not normal division. – Tony Oct 22 '11 at 16:17
  • What I've just done is: (2 * 10) % 11 = 9. Which seems to be giving me my correct answer. – Tony Oct 22 '11 at 16:18
  • Ah, of course. But multiplication and division are not exactly the same thing. You found the correct problem for your solution. – JB Nizet Oct 22 '11 at 16:23
  • So what is the question? You found out that you had to use multiplication instead of division... There is no question. I voted to close. – stivlo Oct 22 '11 at 16:44
  • 1
    @stivlo: the question is about inverting multiplication in [modular arithmetic](http://en.wikipedia.org/wiki/Modular_arithmetic). Because 10 is its own inverse mod 11, multiplying by 10 and dividing by 10 has the same effect in modulo-11 arithmetic. That's why Tony happens to get the correct answer a few comments above. – Luke Woodward Oct 22 '11 at 19:48
  • @LukeWoodward thanks, I got it, then the way to go is define his own class, with its equality operator, it's division, multiplication and so on. – stivlo Oct 23 '11 at 01:46

2 Answers2

5

I think what you are looking for is how to find the multiplicative inverse of a number modulo 11.

10 is its own inverse modulo 11, so it isn't a particularly useful example. Instead, let's find the multiplicative inverse of 7 modulo 11.

To do this, we solve the equation 7a + 11b = 1 for a and b in integers. We use the Euclidean algorithm to find suitable values for a and b. In this case, we can take a = -3 and b = 2. We ignore the value of b, and take a ( = -3) to be the inverse of 7 modulo 11. In modulo-11 arithmetic, 7 times -3 is 1.

If we don't like negative numbers, we can take the inverse of 7 modulo 11 to be 8 ( = -3 + 11) instead.

So, instead of dividing by 7 modulo 11, we multiply by -3, or by 8. For example, in modulo-11 arithmetic, 9 / 7 = 9 * 8 = 72 = 6.

If you only ever have one modulus to work with (e.g. you only ever work modulo 11), it's probably better to calculate a table of multiplicative inverses modulo 11 beforehand and use that in your calculations.

Luke Woodward
  • 63,336
  • 16
  • 89
  • 104
  • Thanks for that Luke, I had a suspicion that's what I was going to have to do. Do you have any advice on generating such a table? I'm guessing I need a multi-dimensional array? – Tony Oct 23 '11 at 09:49
  • 1
    I don't see why you'd need a multidimensional array. If you only ever work modulo 11, calculate of the inverses of 1 to 10 modulo 11 and put them in a 1-dimensional array. If you're working with lots of moduli, or any large moduli, a table won't be such a good idea. – Luke Woodward Oct 23 '11 at 10:37
  • Thanks Luke, I was getting a bit confused with what I was trying to do (it's still quite early for me) :). I've now built an inverse table using an array, and now everything is working as planned. Thanks for your help!! – Tony Oct 23 '11 at 11:09
  • Luke, you've been a massive help, and I was wondering if I could ask of your help for a final time. Simply, I need a way in Java of it outputting 7, when I put in y = -4 % 11. Any suggestions? – Tony Oct 23 '11 at 20:09
  • 1
    @Tony: one possible expression that handles negative numbers as well as positive numbers is `((y % 11) + 11) % 11`. – Luke Woodward Oct 24 '11 at 20:15
1

Not sure if this is what you intend, but...

public static int divmod(int dividend, int divisor, int mod) {
    if (dividend >= divisor)
        return (dividend / divisor) % mod;
    return mod - dividend;
}

Testing it:

divmod(9, 1, 11)  // returns 9
divmod(2, 10, 11) // returns 9
Óscar López
  • 232,561
  • 37
  • 312
  • 386