Блог пользователя shpvb

Автор shpvb, история, 7 лет назад, По-английски

I looked for it and got a source: Link

But this method may not work when the denominator is a multiple of modulus.

For example, (8/8) % 4 should be 1, but applying this approach it gets gcd(4, 8) = 4 and hence find ((8/4)/(8/4))%(4/4) which results in 0.

Similarly for (80/8) % 4 should be 2 but this results in 0.

So what can be a generalized approach towards this?

  • Проголосовать: нравится
  • -10
  • Проголосовать: не нравится

»
7 лет назад, скрыть # |
← Rev. 5  
Проголосовать: нравится +3 Проголосовать: не нравится

Modular division only works when the modulo is prime, because it uses Fermat's little theorem: If p is a prime, then: (a^p)%p=a%p, so a^(p-1)%p=1, then (1/a)%p=a^(p-2)%p, but if the modulo is not a prime, then this equation is not always true, therefore (1/a)%m is not equal to a^(m-2)%m. Imagine if we gave an integer value of (1/2) % 4. Let's denote it with x. Then: 1%4=(2*(1/2))%4=(2*x)%4. But x is an integer, and 2*x is always even, therefore it can't be 1. So you can't define these modular divisions.

»
7 лет назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

Yup. It doesn't make sense to compute for example $$$x=1/2$$$ modulo $$$2$$$, since there's no integer solution of $$$2x=1$$$ modulo $$$2$$$. The most we can do is make sure to reduce the fractions so that gcd(numerator, denominator, modulo) is 1, and when gcd(denominator, modulo) is still greater than 1, then just work modulo lcm(denominator, modulo) if it's small enough.