1

Today I was trying to solve a problem that involved modular arithmetic. I was not able to solve it. So I looked it up on Geeks for Geeks

enter image description here

The above image shows what the author did. I know modular addition for two numbers

(a + b) % m = (a % m + b % m) % m

This works for any positive values of a and b

When I consider the equation the author used in the image.

a % k + b % k = 0

I substituted some random values for a , b and k to see if it really works. It turns out it fails for the input values a = 2, b = 5 and k = 7.

2 % 7 + 5 % 7 = 7 ≠ 0

When I considered the last equation. It worked.

b % k = (k - a % k) % k

(5 % 7) = (7 - 2 % 7) % 7

5 % 7 = 5 % 7

(a + b) % k = c

When I solved the above equation with the same idea as the author, I got

(a + b) % k = c

a % k + b % k = c

b % k = (c - a % k + k) % k

It works for any positive values of a, b, c and k

In the equation,

(a + b) % k = (a % k + b % k) % k

Can I just ignore the last k and proceed while expanding (a + b) % k ?. I wonder how the absence of the last k doesn't affect the final result

1 Answers1

1

No, a = b = 0 is a counterexample.

Indeed, the final formula is incorrect, assuming that % denotes the remainder of truncating division. Let a = 1 and b = -1. (In Python, or for nonnegative integers, it's OK.)

This is why mathematicians prefer to deal in equivalence mod K, which avoids the issue of where to put the mod operator.

David Eisenstat
  • 64,237
  • 7
  • 60
  • 120