1
$\begingroup$

As by given algorithm (a/b)%c = ((a%c)*((b^-1)%c))%c

but for example (9999/99)%9 = 2 , but by given algorithm it turn out to be 0.

this algorithm is based on the below link. https://www.hackerearth.com/practice/math/number-theory/basic-number-theory-1/tutorial/#c85200

please provide another algorithm apart from mentioned above as it is giving wrong answer for cases as i mentioned above.

  • 0
    I think you misunderstood what `b^-1` stands for. From the link you posted: "*$b^{−1}$ is the* ***multiplicative modulo inverse*** *of b and c*".2017-02-05
  • 0
    @dxiv nope i knew that. modulo inverse of (99,9) will turns out to be 0 and also 9999%9=0 so it turns out to be (9999/99)%9 = 0 but it should be 2. So this algorithm is wrong in such cases. Please provide any other algorithm that would be helpful.2017-02-05
  • 0
    `modulo inverse of (99,9) will turns out to be 0` That's wrong, because $99 \cdot 0 \not \equiv 1 \pmod{9}\,$. I suggest you first check out section #5 on that linked page.2017-02-05
  • 0
    @dxiv i run the code its giving 0. Anyhow 0*modulo inverse of (99,9)=0. so it is not gonna turned out to 2.2017-02-05
  • 2
    @user6439179 It is *not* $0\,$, it just doesn't exist in this case. From the section #5 I mentioned before: "*An inverse exists* ***only*** *when A and M are coprime*". Since $\gcd(99,9)=9 \gt 1$ the inverse doesn't exist, so the given formula does not apply here.2017-02-05
  • 0
    @user6439179 dxiv is right, the inverse of $99$ modulo $9$ doesn't exist, so you can't use this formula. It should not give you $0$.2017-02-05

1 Answers 1

2

Let $d = GCD(a, b), e=\frac{a}{d}, f=\frac{b}{d} $

Now calculate (e/f)%c by a previous algorithm