0

I know that (a/b)mod M = ab^-1 mod M

and also that when M is prime then b^-1 = b^(M-2)

I have to calculate (121/2)mod M where M = 1000000007 (1e9 + 7)

Using simple division: (121/2)modM = (60)mod M = 60%M = 60

Using modular Inverse: (121/2)mod M = ( (121 mod M ) * ( 2 ^(M-2) mod M) ) mod M.

2 ^(M-2) mod M here is 500000004 (link: http://www.cs.princeton.edu/~dsri/modular-inversion-answer.php?n=2&p=1000000007)

so the above expression becomes (121 mod M * 500000004)mod M = 60500000484 mod M = 500000064

What am i possibly doing wrong?

lassaendie
  • 105
  • 8

1 Answers1

3

You can't perform integer division and expect to get the same result. You calculation should really be:

121/2 (mod M) = 60 + 1/2 (mod M) = 60 + 50000004 = (mod M) = 50000064

and not

121/2 (mod M) = 60 (mod M)

Since there is no definition of a floor function from the rational numbers Q into group Z_n (only defined for rational numbers into natural numbers, floor: Q -> N) you should treat the divisions in Z_n as you would with rationals, that is using remainders.

sve
  • 4,336
  • 1
  • 19
  • 30
  • I am doing integer division because that is how the solution is to be derived for the problem I am solving. I have to output the answer modulo M. The final answer is of the form floor(A/2). The numerator is formed after after a series of steps of addition and multiplication so I take mod M at every step with the numerator. At the end I only have A%M and not the actual A – lassaendie Oct 03 '15 at 23:13
  • @lassaendie could you link the problem – sve Oct 03 '15 at 23:16
  • I am afraid I cannot because I have to solve it myself. It would be cheating on my part to upload it – lassaendie Oct 03 '15 at 23:17
  • @lassaendie so what exactly is the your problem then? are you asking why your calculations yield different results? the answer for `121/2 (mod M)` is indeed `500000064` – sve Oct 03 '15 at 23:19
  • nope. answer for 121/2 should be 60. But as you said 121/2 mod M is indeed 500000064 then I suppose I am stuck. I am 100% sure the answer is right. :/ Or maybe there is another way to approach the same problem. Is there a way I can communicate the problem to you without disclosing it in public? – lassaendie Oct 03 '15 at 23:23
  • @lassaendie `answer for 121/2 should be 60` why do you think so? who says so? – sve Oct 03 '15 at 23:26
  • Because The answer is floor(121/2) – lassaendie Oct 03 '15 at 23:27
  • @lassaendie `floor(121/2) = 60` but not under `mod M`. You **can't** apply `floor` function when you using `mod M`. The floor function is defined for natural numbers as `floor: N -> N` but when using `mod M` you work in the set `Z_M` which completely different set from `N` – sve Oct 03 '15 at 23:31
  • That is exactly what i require. – lassaendie Oct 03 '15 at 23:33
  • @lassaendie `That is exactly what i require.`. And what is that? Coming up with `floor` function for `Z_M` – sve Oct 03 '15 at 23:35
  • 1
    It took me some time to understand the answer. I think it would be worth to say that exact divisions behave exactly like the integer counter-part. The problem is always the remainder. – Juan Lopes Oct 04 '15 at 02:05