6

I need to calculate (a/b) mod m where a and b are very large numbers.

What I am trying to do is to calculate (a mod m) * (x mod m), where x is the modular inverse of b.

I tried using Extended Euclidean algorithm, but what to do when b and m are not co-prime? It is specifically mentioned that b and m need to be co-prime.

I tried using the code here, and realized that for example: 3 * x mod 12 is not at all possible for any value of x, it does not exist!

What should I do? Can the algorithm be modified somehow?

Lazer
  • 90,700
  • 113
  • 281
  • 364

3 Answers3

7

Yep, you are in trouble. x has no solution in b*x = 1 mod m if b and m have a common divisor. Similarly, in your original problem a/b = y mod m, you are looking for y such that a=by mod m. If a is divisible by gcd(b,m), then you can divide out by that factor and solve for y. If not, then there is no y that can solve the equation (i.e. a/b mod m is not defined).

Keith Randall
  • 22,985
  • 2
  • 35
  • 54
  • I'm not sure what you mean. If a is divisible by gcd(b,m), then you can solve it. Otherwise, the divide operation isn't even defined. You'd need to figure out another way to achieve your ultimate objective without modular division. – Keith Randall Oct 05 '09 at 20:29
  • I mean, what can be the other ways to "achieve my ultimate objective without modular division"? – Lazer Oct 05 '09 at 22:53
  • @eSKay your question doesn't make sense. What is your ultimate objective? – rlbond Oct 06 '09 at 00:14
  • 1
    Why are you doing modular division in the first place? I presume there is some task (a.k.a. ultimate objective) you're trying to perform using modular division as a subroutine. What is that task? Methinks that whatever it is, modular division probably isn't the answer. – Keith Randall Oct 06 '09 at 03:23
  • 1
    @Keith Randall yep, you are right. what I am trying to do is to caculate the mod of a series of the form 1+x+x^2+x^3+...+x^n % m. I mentioned a/b form because I tried to use the geometric sum formula. iterating the entire series id not an option as n is very very large (~10^17) – Lazer Oct 06 '09 at 11:07
  • You could evaluate k_i = 1 + x + x^2 + ... + x^{i-1} % m with a recursive formula: k_1 = 1 k_2i = k_i + x^i * k_i % m Compute x^i with a repeated squaring algorithm (http://en.wikipedia.org/wiki/Repeated_squaring). – Keith Randall Oct 06 '09 at 15:38
  • Actually, now that I look at it a bit more you might still be ok. It should always be the case that a is divisible by gcd(b,m) with the numbers you're using (a = 1-x^{n+1}, b = 1-x). However, if gcd(b,m)>1, then the results of the division are not unique - you identify the result mod m/gcd(b,m). I don't know if there's an easy way to figure out which of the gcd(b,m) possibilities is the right one without computing your sum from scratch. – Keith Randall Oct 06 '09 at 20:08
  • @Lazer: If calculating a geometric series modulo m is your objective, then [this link](http://www.codechef.com/wiki/tutorial-just-simple-sum#Back_to_the_geometric_series) could be useful. – Craig McQueen Oct 08 '13 at 10:30
2

The reason that b and m have to be coprime is because of the Chinese Remainder Theorem. Basically the problem:

3 * x mod 12

Can be thought of as a compound problem involving

3*x mod 3 and 3*x mod 4 = 2^2

Now if b is not coprime to 12, this is like trying to divide by zero. Thus the answer doesn't exist!

This is due to field theory in abstract algebra. A field is basically a set which has addition, subtraction, multiplication, and division well-defined. A finite field is always of the form GF(p^n), where p is prime and n is a positive integer, and the operations are addition and multiplication modulo p^n. Now, 12 is not a prime power, so your ring is not a field. Thus this problem can't be solved for any b which is not coprime to m.

rlbond
  • 65,341
  • 56
  • 178
  • 228
1

Check this: http://www.math.harvard.edu/~sarah/magic/topics/division It might help. It explains methods of modular division.

avd
  • 13,993
  • 32
  • 78
  • 99