Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is really a programming question that I'm unable to solve because of a math question. I'm having a problem understanding the rules of distribution with modular arithmetic.

I have two expressions:

a = q mod c + (n - 1) mod c
b = q mod c - (n - 1) mod c

I made the mistake of concluding that the expressions were equivalent to:

a = (q + n - 1) mod c
b = (q - n + 1) mod c

What would the equivalent equations be?

share|cite|improve this question
up vote 1 down vote accepted

Mathematically, they're equivalent, but I think the difference is that in computers the first set you gave is going to potentially evaluate to outputs larger than $c$, while in mathematics we would automatically convert the output of the first two equations mod $c$ again so that the result is in $\{0\dots c-1\}$.

If you take the results of the top two equations mod $c$ again, you should get answers that match the outputs of the second box.

share|cite|improve this answer
It looks like that was exactly what was happening. Thanks for the answer! – Pé de Leão Jun 8 '13 at 16:57

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.