# Not understanding modulo

I'm not sure if I'm in the right place, but I'll give it a try! I'm very bad with mathematics even though it's pretty interesting. Well, for Java programming we have to use the modulo operator, but I just don't get the modulo it self. I hope any of you would be able to explain in simple words and with an example what it does.

-
It means the remainder on division. For example $18 \equiv 3 (mod 5)$ because 5 goes into 18 3 times with remainder 3. Another example $41 \equiv 2 (mod 3)$ because 3 goes into 41 13 times with 2 remainder. – Simon Hayward Nov 23 '12 at 10:45
You should remember that % doesn't work the way you might expect for negative integers (in C, C++, C#, Java, etc.). – wj32 Nov 23 '12 at 10:45

If you mean Modulo operation like "3 mod 2" then this is just the remainder when you divide 3 by 2. 3 divided by 2 is 1 with a remainder of 1, so "3 mod 2" is 1.

In order to understand this, you must first be able to do long division by hand (using calculators doesn't work since you get decimal numbers). If you know long division, then it's really easy to calculate any number modulo any other number (natural numbers only).

For example 4 mod 2 is 0 because 4 is divisible by 2.

16 mod 3 is 1 because 16 divided by 3 is 5 with a remainder of 1. In other words 3 goes into 16 5 times and there is a remainder of 1.

It's all about remainders! Once you get comfortable with the definition you can learn some algebraic rules of manipulating these remainders.

-
This explanation did it for me! Thank you very much! – Fabian Pas Nov 23 '12 at 11:45

When you first learned about division, it might even have been before you learned about fractions. If you are dividing integers ("whole numbers") then instead of writing $14 \div 3 = 4\frac{2}{3}$ you can write $$14 \div 3 = 4 \text { remainder } 2.$$

The modulo operator is just the function that takes two integers and gives the remainder when the first is divided by the second: $$14 \mod 3 = 2, \quad$$

The other operator of integer division is usually called $\text{div}$, and $x \text{ div } y$ is defined, as you'd expect, to be the greatest $n$ such that $y \times n \leq x$. So for example $14 \text{ div }3 = 4$. And we can define $\text{mod}$ formally as: $$x \mod y := x- (y \times (x \text{ div } y)).$$

Is this clear?

-

Let $a$ > $m$ be integer numbers. Then you have $$a = q \cdot m + r$$ where $q$ and $r$ are unique integers (maybe $q=0$), and $r$ is called the remainder. By definition $$r = a \mod m$$ or if you prefer $a$ % $m = r$.

An example: $a=7, m = 3$. We have $$7 = 2\cdot 3 + 1$$ so we see that $7 = 1 \mod 3$.

Hope this helps!

-
I think you should stress out the uniqueness of $q$ abd $r$, at least with a little reference. – Pedro Tamaroff Nov 23 '12 at 11:54
i agree with that very first equation, but for a non-mathematician it might seem unnatural, and of course it is of huge importance in ring theory so some explanation could be useful – akkkk Nov 23 '12 at 13:37