How to calculate (a−b)modn\,(a-b)\bmod n\, and −bmodn {-}b \bmod n [closed]

Consider the following expression:

(a - b) mod N

Which of the following is equivalent to the above expression?

1) ((a mod N) + (-b mod N)) mod N

2) ((a mod N) - (b mod N)) mod N

Also, how is (-b mod N) calculated, i.e., how is the mod of a negative number calculated?

Thanks.

Answer

Other answers have addressed the immediate question, so I’d like to address a philosophical one.

I think that the way you’re thinking of “mod” is a bit misleading. You seem to be thinking of “mod” as an operator: so that “13 mod 8” is another way to write the number “5”. This is the way that modulo operators often work in programming languages: in Python you can write “13 % 8” and get back the number 5.

Mathematically, though, I think it is better to think of “mod 8” as an adverb modifying “=”: when we say “5 = 13 (mod 8)” we are really saying “5 is equal to 13, if you think of equality as working modulo 8″. When you think of “mod” this way, it doesn’t really make sense to ask about the expression “((a mod N) + (-b mod N)) mod N”: it’s not even really an expression, under this interpretation.

I’m not trying to say that you are wrong for thinking of “mod” as an operation, because the operation of “taking a residue mod $m$” is a useful operation. However, I think it is also useful to keep the other meaning of “mod” in mind.

(After writing this answer I see that the question was posted more than a year ago. Well, maybe someone else will find this helpful.)

Attribution
Source : Link , Question Author : J.P. , Answer Author : Gregory J. Puleo