elementary number theory - Help me understand division in modular arithmetic - Mathematics Stack Exchange
algorithm - Modulo of Division of Two Numbers - Stack Overflow
What's the difference between % (modulo) and // (floor division)
Modulus Operator
Videos
There is a simple definition of the operator, in that if we have
, then we may write
for some
.
We reduce to a value in
and fix
accordingly.
So:
for some .
And:
$$a \pmod n + b \pmod n = a+k_an + b+k_bn = (a+b) + (k_a+k_b)n = (a+b) + kn$$
So these two are the same.
With division, the two resulting formula are not the same:
$$\frac{a \pmod n}{b \pmod n} = \frac{a + k_an}{b + k_bn}$$
I'll try my best to answer concisely:
It is not a condition for
to be in
but since every integer is equivalent to one, it makes sense to use numbers in this range
When I studied this, I liked to think that rather than division of
by
, we multiply by the inverse of
. If
there is no inverse (it is 'like' dividing by zero in a naïve sense)
To find the inverse of there is the extended Euclidean algorithm.
Yes, but it's different:
(a - b) mod p = ((a mod p - b mod p) + p) mod p
(a / b) mod p = ((a mod p) * (b^(-1) mod p)) mod p
Where b^(-1) mod p is the modular inverse of b mod p. For p = prime, b^(-1) mod p = b^(p - 2) mod p.
Edit:
(N*(N^2+5)/6)%P
You don't need any modular inverses from this. Just simplify the fraction: N or N^2+5 will be divisible by 2 and 3. So divide them and then you have (a*b) mod P.
Vlad's answer is correct:
(a - b) mod p = ((a mod p - b mod p) + p) mod p
(a / b) mod p = ((a mod p) * (b^(-1) mod p)) mod p
These and some other operations are outlined here in the Equivalencies section.
Just want to let you know that this will work not only for prime number p. The first one will work for any p. The second one will work for any p, where b^(-1) or modular inverse is defined.
The modular inverse can be calculated with extended Euclidian algorithm
Hello, Can someone please explain the me the difference between those two arithmetic signs because am new to programming python:)