Wikipedia:Reference desk/Archives/Mathematics/2020 August 12
Appearance
Mathematics desk | ||
---|---|---|
< August 11 | << Jul | August | Sep >> | Current desk > |
Welcome to the Wikipedia Mathematics Reference Desk Archives |
---|
The page you are currently viewing is a transcluded archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages. |
August 12
[edit]Modular Arithmetic
[edit]How can I calculate modular subtraction if a is greater than b.
— Preceding unsigned comment added by 185.181.55.124 (talk) 11:04, 12 August 2020 (UTC)
- Courtesy link: Modular arithmetic -- ToE 12:53, 12 August 2020 (UTC)
- For modulus n, if your difference is negative, then add as many multiples of n as is necessary to make it nonnegative. Specifically, if 0 ≤ b < a < n, then the addition of a single copy of n suffices:
- (b - a) mod n = b - a + n
- This is similar to how with addition you subtract an n if a + b ≥ n.
- -- ToE 12:53, 12 August 2020 (UTC) Edit: Restated in terms of Modulo operator. -- ToE 13:11, 12 August 2020 (UTC) Edited again to correct order in inequality. Thanks Lambiam. -- ToE 15:31, 12 August 2020 (UTC)
- Strictly speaking, the equality holds in modular arithmetic, also when is greater than . But that is apparently not the answer you are looking for; you want a solution to the equation in which the right-hand side is normalized, that is, such that . Recalling the definition of the congruence class denoted by , we see that it is the set
- so that the question now is: which of these infinitely many elements lies in the range from to ? (I am assuming here that the modulus .) Each element of this set is of the form , in which is an integer. So to find subject to the constraint that , we need to find satisfying , or, equivalently,
- There is only one integer satisfying both inequations, and if some does, then will also satisfy the left inequation and therefore fail the other one. This means that is the least integer satisfying , and so is given by , in which denotes the ceiling function. This is of course equivalent to the much simpler answer provided in the first response, but gives an explicit formule for the general case, together with the mathematical reasoning behind it. --Lambiam 15:06, 12 August 2020 (UTC)
- There is a lot of confusion between congruence classes and the remainder function, exacerbated by the fact that computer notation often uses 'mod' to denote the remainder. This can have the effect of making modular arithmetic seem difficult. But actually people do modular arithmetic (addition and subtraction at least) without thinking about it when dealing with time. I assume most people can figure out how many hours it is from 10 o'clock to 2 o'clock without thinking about equivalence classes and residue systems; see the lead section of the article on modular arithmetic. --RDBury (talk) 21:48, 12 August 2020 (UTC)