How does a modulo operation work when the first number is smaller?

Viewed 66385

I'm messing with the modulo operation in python and I understand that it will spit back what the remainder is.

But what if the first number is smaller than the second?

for instance

2 % 5 the answer is 2.

How does that work?

2/5 = .4

19 Answers

If the first number is smaller, then the answer is that first number again.

Because the second number is larger, it 'goes into' the first number zero times and the remainder is the entirety of this first number.

edit: revisiting this thread, I had to remember what this operator was for. I referred to this other thread here:

Recognizing when to use the modulus operator

Just as a follow up for us non-math-brain people out there, I think part of the problem in understanding this is that the concept is often simplified as "what's left over when you divide x by y", which, when x is smaller, is nothing, aka 0. I more fail proof way might be to say

  1. How many times does y fully go into x?
  2. Take that number, and multiply it by y to get as close to x as possible
  3. Now subtract what you get from step 2 from x, that's your modulo.

So in 2 (as x) % 5 (as y):

  1. 5 goes into 2 fully no times at all, so, 0
  2. 0 (the outcome of the step above) multiplied by 5(aka y) is 0
  3. 2(aka x) - 0 (the product from the step above) is 2

To understand modular arithmetic, I suggest you go over to Khan Academy and read their post about it. They also have interactive practice questions on the same page. Here's the link: https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/what-is-modular-arithmetic

In short:

Use the following equation:

A = BQ + R

A is the dividend

B is the divisor

Q is the quotient

R is the remainder, and is the result for a modulo.

Q = (A/B)

Keep in mind that Q always goes to the closest smallest integer. So if Q = 0.2, then Q = 0.0. If Q = -1.2, then Q = -2.0.

If Q doesn't have decimals then your final answer is 0 (R = 0).


For your question:

Q = (2/5) = 0.4, so Q = 0.

Plug that into 'A = BQ + R':

2 = 5*0 + R

So, R = 2.


Hope this helps. As I said you can read more about on Khan Academy. Here's the link: https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/what-is-modular-arithmetic

In case i.e. 2%5 = 2: In real math remainder is 0 and quotient is 0.4 but in programming, it see 2 is less than 5 means 5*1=5, so it go low and just divide it by 0 so it become less which is 0. Hence remainder become 2 because 2-0=2 ;)

For example, 2 % 5 = 2

Since we get 0 if we do 2 divided by 5, the answer will be 2 as that should be the number(remainder) required to reach the number that is divided(in this case 2) from 0.

To further help your understanding:

125 % 130 = 125
50 % 51 = 50
12345 % 12346 = 12345

Modulo works by giving the remainder after division, one thing it's useful for is
-Finding if a number is even or not
Code sample:

// 4 % 2 means "2 divided by 2 is what, and what is the remainder? if I have a remainder, return it or else return 0"
if(4 % 2 == 0) {
  alert("2 is even");
} else {
  alert("2 is odd");
}

So if 4 % 2 has a remainder of 0, it's even or else it's odd.

Related