[modulo] Why does 2 mod 4 = 2?

I'm embarrassed to ask such a simple question. My term does not start for two more weeks so I can't ask a professor, and the suspense would kill me.

Why does 2 mod 4 = 2?

This question is related to modulo

The answer is


MOD is remainder operator. That is why 2 mod 4 gives 2 as remainder. 4*0=0 and then 2-0=2. To make it more clear try to do same with 6 mod 4 or 8 mod 3.


This could be a good time to mention the modr() function. It returns both the whole and the remainder parts of a division.

print("\n 17 // 3 =",17//3," # Does the same thing as int(17/3)")
print(" 17 %  3 =",17%3," # Modulo division gives the remainder.")
whole, remain = divmod(17,3)
print(" divmod(17,3) returns ->",divmod(17,3),end="")
print(" because 3 goes into 17,",whole,"times with a remainder of",remain,end=".\n\n")

For:

2 mod 4

We can use this little formula I came up with after thinking a bit, maybe it's already defined somewhere I don't know but works for me, and its really useful.

A mod B = C where C is the answer

K * B - A = |C| where K is how many times B fits in A

2 mod 4 would be:

0 * 4 - 2 = |C|

C = |-2| => 2

Hope it works for you :)


The modulo operator evaluates to the remainder of the division of the two integer operands. Here are a few examples:

23 % 10 evaluates to 3 (because 23/10 is 2 with a remainder of 3)
50 % 50 evaluates to 0 (50/50 is 1 with a remainder of 0)
9 % 100 evaluates to 9 (9/100 is 0 with a remainder of 9)

Someone contacted me and asked me to explain in more details my answer in the comment of the question. So here is what I replied to that person in case it can help anyone else:

The modulo operation gives you the remainder of the euclidian disivion (which only works with integer, not real numbers). If you have A such that A = B * C + D (with D < B), then the quotient of the euclidian division of A by B is C, and the remainder is D. If you divide 2 by 4, the quotient is 0 and the remainder is 2.

Suppose you have A objects (that you can't cut). And you want to distribute the same amount of those objects to B people. As long as you have more than B objects, you give each of them 1, and repeat. When you have less than B objects left you stop and keep the remaining objects. The number of time you have repeated the operation, let's call that number C, is the quotient. The number of objects you keep at the end, let's call it D, is the remainder.

If you have 2 objects and 4 people. You already have less than 4 objects. So each person get 0 objects, and you keep 2.

That's why 2 modulo 4 is 2.


For a visual way to think about it, picture a clock face that, in your particular example, only goes to 4 instead of 12. If you start at 4 on the clock (which is like starting at zero) and go around it clockwise for 2 "hours", you land on 2, just like going around it clockwise for 6 "hours" would also land you on 2 (6 mod 4 == 2 just like 2 mod 4 == 2).


To answer a modulo x % y, you ask two questions:

A- How many times y goes in x without remainder ? For 2%4 that's 0.

B- How much do you need to add to get from that back to x ? To get from 0 back to 2 you'll need 2-0, i.e. 2.

These can be summed up in one question like so: How much will you need to add to the integer-ish result of the division of x by y, to get back at x?

By integer-ish it is meant only whole numbers and not fractions whatsoever are of interest.

A fractional division remainder (e.g. .283849) is not of interest in modulo because modulo only deals with integer numbers.


Modulo (mod, %) is the Remainder operator.

2%2 = 0 (2/2 = 1 remainder 0)
1%2 = 1 (1/2 = 0 remainder 1)
4%2 = 0 (4/2 = 2 remainder 0)
5%2 = 1 (5/2 = 2 remainder 1)

Much easier if u use bananas and a group of people.

Say you have 1 banana and group of 6 people, this you would express: 1 mod 6 / 1 % 6 / 1 modulo 6.

You need 6 bananas for each person in group to be well fed and happy.

So if you then have 1 banana and need to share it with 6 people, but you can only share if you have 1 banana for each group member, that is 6 persons, then you will have 1 banana (remainder, not shared on anyone in group), the same goes for 2 bananas. Then you will have 2 banana as remainder (nothing is shared).

But when you get 6 bananas, then you should be happy, because then there is 1 banana for each member in group of 6 people, and the remainder is 0 or no bananas left when you shared all 6 bananas on 6 people.

Now, for 7 bananas and 6 people in group, you then will have 7 mod 6 = 1, this because you gave 6 people 1 banana each, and 1 banana is the remainder.

For 12 mod 6 or 12 bananas shared on 6 people, each one will have two bananas, and the remainder is then 0.


When you divide 2 by 4, you get 0 with 2 left over or remaining. Modulo is just the remainder after dividing the number.


I think you are getting confused over how the modulo equation is read.

When we write a division equation such as 2/4 we are dividing 2 by 4.

When a modulo equation is wrote such as 2 % 4 we are dividing 2 by 4 (think 2 over 4) and returning the remainder.


This is Euclid Algorithm.

e.g

a mod b = k * b + c => a mod b = c, where k is an integer and c is the answer

4 mod 2 = 2 * 2 + 0 => 4 mod 2 = 0

27 mod 5 = 5 * 5 + 2 => 27 mod 5 = 2

so your answer is

2 mod 4 = 0 * 4 + 2 => 2 mod 4 = 2


Modulo is the remainder, not division.

2 / 4 = 0R2
2 % 4 = 2

The sign % is often used for the modulo operator, in lieu of the word mod.

For x % 4, you get the following table (for 1-10)

 x x%4
------
 1  1
 2  2
 3  3
 4  0
 5  1
 6  2
 7  3
 8  0
 9  1
10  2

I was confused about this, too, only a few minutes ago. Then I did the division long-hand on a piece of paper and it made sense:

  • 4 goes into 2 zero times.
  • 4 times 0 is 0.
  • You put that zero under the 2 and subtract which leaves 2.

That's as far as the computer is going to take this problem. The computer stops there and returns the 2, which makes sense since that's what "%" (mod) is asking for.

We've been trained to put in the decimal and keep going which is why this can be counterintuitive at first.


mod means the reaminder when divided by. So 2 divided by 4 is 0 with 2 remaining. Therefore 2 mod 4 is 2.


Modulo is the remainder, expressed as an integer, of a mathematical division expression.

So, lets say you have a pixel on a screen at position 90 where the screen is 100 pixels wide and add 20, it will wrap around to position 10. Why...because 90 + 20 = 110 therefore 110 % 100 = 10.

For me to understand it I consider the modulo is the integer representation of fractional number. Furthermore if you do the expression backwards and process the remainder as a fractional number and then added to the divisor it will give you your original answer.

Examples:

    100
(A) ---  =  14 mod 2
     7

    123
(B) ---  =  8 mod 3
     15

     3
(C) ---  =  0 mod 3
     4

Reversed engineered to:

                        2      14(7)    2       98    2     100
 (A) 14 mod 2  =  14 + ---  =  ----- + ---  =  --- + ---  = ---
                        7        7      7       7     7      7

                      3      8(15)    3      120    3      123
 (B) 8 mod 3  =  8 + ---  =  ----- + ---  =  --- + ---  =  ---
                      15       15     15      15    15      15

                      3       3
 (B) 0 mod 3  =  0 + ---  =  ---
                      4       4

Mod operation works with reminder.

This is called modular arithmetic.

 a==b(mod m) 
 then m|(a-b)
 a-b=km 
 a=b+km
 So, 2=2+0*4

2 / 4 = 0 with a remainder of 2