Calculating Modulo for -3 and 26: Understanding the Remainder Calculation

  • Thread starter Thread starter shivajikobardan
  • Start date Start date
  • Tags Tags
    Calculation
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
22 replies · 4K views
shivajikobardan
Messages
637
Reaction score
54
Summary: calculating modulo i.e remainder when -3 divided by 26

The answer seems to be (26-3)mod26=23. But I'm not sure how? Is there some rule like that? I don't quite get it.
 
Physics news on Phys.org
shivajikobardan said:
Summary: calculating modulo i.e remainder when -3 divided by 26

The answer seems to be (26-3)mod26=23. But I'm not sure how? Is there some rule like that? I don't quite get it.
What do you think it should be?
 
shivajikobardan said:
Summary: calculating modulo i.e remainder when -3 divided by 26

The answer seems to be (26-3)mod26=23. But I'm not sure how? Is there some rule like that? I don't quite get it.

Related question: What's the integer part of -3/26? Is it [tex] \left\lfloor - \frac{3}{26} \right\rfloor = -1\qquad\mbox{or}\qquad-\left\lfloor \frac{3}{26} \right\rfloor = 0?[/tex] If you use the first definition, then the remainder is [tex] R(x) = x - 26\left\lfloor \frac{x}{26}\right\rfloor[/tex] and you would have [tex]R(-3) = -3 - 26(-1) = 23.[/tex] Alternatively if you use the second approach then [tex] R(x) = x - 26\operatorname{sgn}(x)\left\lfloor \frac{|x|}{26}\right\rfloor[/tex] and you would get [itex]R(-3) = -3[/itex].
 
  • Like
Likes   Reactions: topsquark
PeroK said:
What do you think it should be?
I think it should be -3 itself. As it's not divisble by 26. Just like if it was 3%26, it'd be 3.
 
shivajikobardan said:
Summary: calculating modulo i.e remainder when -3 divided by 26

The answer seems to be (26-3)mod26=23. But I'm not sure how? Is there some rule like that? I don't quite get it.
##a \equiv b ## (mod ## n ##) means that ##(a-b)## is an integer multiple of ##n## i.e. ##a-b = kn## for some integer ##k##. You can also phrase this as a and b have same remainer when dividing with n: a = pn + ra and b = qn + rb. But if ra = rb then you have that a-b = (p-q)n = kn i.e. a-b is an integer multiple of n.

Therefore, you can always add or subtract any integer muptiple of ##n## to either ##a## or ##b##, for instance
if ##a \equiv b ## (mod ## n ##) , then:
##a \equiv b + n ## (mod ## n ##)
##a + n\equiv b ## (mod ## n ##)
etc
So in your case, you can add 26 to -3, you can add 52, subtract 26, subtract 52 and so on, and still have the same remained when dividing with 23
 
Last edited:
  • Like
Likes   Reactions: topsquark
shivajikobardan said:
I think it should be -3 itself. As it's not divisble by 26. Just like if it was 3%26, it'd be 3.
What are the possible answers for a "remainder" on division by 26? Is it the set ##\{-25, -24 \dots -1, 0, 1, \dots 24, 25\}##?

PS how is "remainder" defined?
 
  • Like
Likes   Reactions: malawi_glenn
malawi_glenn said:
##a \equiv b ## mod##(n)## means that ##(a-b)## is an integer multiple of ##n## i.e. ##a-b = kn## for some integer ##k##.
I think you mean ##a \equiv b \pmod n ## means that ##(a-b)## is an integer multiple of ##n## i.e. ##a-b = kn## for some integer ##k##. The placing of the parentheses (in this case automatic through use of the \pmod symbol) is important.

However the OP is asking about something subtly different, the value of b where ## b = a \text{ mod } n ##. This is defined as the unique integer ## b \in \{0 \dots n-1\} ## for which ##a \equiv b \pmod n ## is true: for positive integers this is the same as the remainder on division by ## n ## but for negative integers this is not the case.
 
Last edited:
  • Like
Likes   Reactions: malawi_glenn, vela and topsquark
If we are talking about modular arithmetic, then technically ##-3## is the additive inverse of ##3##, which is ##23## in this case. So:$$-3 \equiv 23 \ (mod \ 26)$$The simpler concept of a remainder is usually defined as a non-negative integer (although not always). In the usual case, we would have:
$$-3 = (-1 \times 26) + 23$$
 
  • Like
Likes   Reactions: SammyS, FactChecker, topsquark and 1 other person
As a simple analogy, I like to think in terms of a 12 hour clock.

Clocks read hours as 1,2,3,4,5,6,7,8,9,10,11,12 only.

Hence -3 hours would be 9 o'clock.
 
  • Like
Likes   Reactions: malawi_glenn and shivajikobardan
1660752052057.png
 
  • Like
Likes   Reactions: shivajikobardan
pbuk said:
However the OP is asking about something subtly different, the value of b where ## b = a \text{ mod } n ##. This is defined as the unique integer ## b \in \{0 \dots n-1\} ## for which ##a \equiv b \pmod n ## is true: for positive integers this is the same as the remainder on division by ## n ## but for negative integers this is not the case.

Never seen ## b = a \text{ mod } n ## in my life, that you use an equal sign that is.
I have seen it when you deal with equivalence classes though.

I have only seen it being stated like this
"Find the smallest positive integer ##x## that solves ##x \equiv -18 \pmod {12}##"
or
"What is the smallest positive remainder when -18 is divided by 12" or "principal remainder"
 
malawi_glenn said:
Writing a = b (mod n) is an abomination!
Yes it would be, but no-one has done that. As I said, parentheses (and the equivalence relation vs equality) are important here to distinguish between the two forms.
 
pbuk said:
Yes it would be, but no-one has done that. As I said, parentheses (and the equivalence relation vs equality) are important here to distinguish between the two forms.
You did so in post 7.
pbuk said:
However the OP is asking about something subtly different, the value of b where b=a mod n.
Never seen that notation.
 
malawi_glenn said:
You did so in post 7.
No I didn't.

malawi_glenn said:
Never seen that notation.
You already said that in #12, which is why I explained it to you.
 
shivajikobardan said:
yes it's CS. didn't know math and CS were different for modulo.
How a computer language works is at the discreption of the developers of that language. In Python, for example:
$$-3\%26 = 23$$Which is what I would have hoped.
 
  • Like
Likes   Reactions: malawi_glenn
More correctly in Python the expression would be:

Python:
-3%26 == 23

as single equals is for assignment x-y ie assign the value of y to x.

and double equals is for equality as in x==y meaning the value of x is equal to the value of y

An example python program:

Python:
for x in range(-50,50):
    y=x%26
    print("%d  %d"%(x,y))

One thing to note in python is that the % operator takes on different uses depending on how it is used:
- as a modulo operator y=x%26
- as a format specifier as in %d for printing an integer
- as a string formatting operator to apply the tuple to the format string "%d %d"
 
  • Sad
  • Like
Likes   Reactions: malawi_glenn and PeroK