# Elementary question on modular arithmetic

I know, this is very simple and dumb question, i just cannot come to understand, the problem is:

Why and how happens this in mathematics?

$$-5 \pmod 4 = 3$$

I know how to get this for positive numbers, but how does it work for negative ones.

I need the explanation of what happens in the background when solving this. Is it the distance from $0$ to $4$?

-

1
by the definition of modular arithmetic, 4 must divide $-5 - x$. Any x for which $4|(-5 - x)$ is a solution. Since we usually want to end up with a least positive choice for x, if we let x = 3, we have $4|(-5 - 3)$, i.e., $4|-8$. – amWhy Nov 4 '12 at 18:52
Add or substract multiples of $4$ until you get back into the "ballpark". – littleO Nov 4 '12 at 18:53

## 3 Answers

Since you seem to be using "mod" as a binary operator rather than as the context for a congruence relation, let's define "mod" precisely: assuming $b > 0$, $$a \bmod b = a - b\lfloor a/b \rfloor$$

That is, $a \bmod b$ denotes the distance to $a$, from the largest multiple of $b$ that is not greater than $a$. If you imagine the "number line" with the multiples of $b$ all marked out, then $a \bmod b$ is the distance to the point $a$ from the closest marked point on its left.

In your particular case, of $-5 \bmod 4$, note that the list of all integer multiples of $4$ is: $$\dots, -20, -16, -12, -8, -4, 0, 4, 8, 12, 16, 20, 24, \dots$$ In this list, the largest number (multiple of $4$) that is to the left of $-5$ is $-8$. And the distance from $-8$ to $-5$ is $3$; that is why we say that $-5 \bmod 4 = 3$.
(This is exactly the same way we would calculate $5 \bmod 4$: in the list, the largest number that is to the left of $5$ is $4$, and the distance from $4$ to $5$ is $1$, so we say $5 \bmod 4 = 1$.)

-

Thanks ShreevatsaR, the best explanation! thanks a lot – doniyor Nov 4 '12 at 19:03

$n \mod m$ is the distance between $n$ and $k$: the largest multiple of $m$ such that $k <= n$

In this case, the largest multiple of 4 which is not larger that -5 is -8. Therefore $$-5 \mod 4 = |-5 - (-8)| = 3$$

-

great @pedrosorio, thanks a lot. – doniyor Nov 4 '12 at 19:08

$$c \equiv b \pmod{a}$$ is a short hand notation to denote $a \vert (c-b)$ (or) equivalently $b$ is the remainder when $c$ is divided by $a$. Typically, for convenience people have $b \in \{0,1,2,\ldots,a-1\}$.

In your case, you want to evaluate $-5 \pmod{4}$. By that I assume you want to mind $b \in \{0,1,2,3\}$ such that $-5 \equiv b \pmod{4}$ i.e. we want to find $b \in \{0,1,2,3\}$ such that $4 \vert (-5-b)$. Since $4 \vert (-5-b)$, we have that $4 \vert (5 + b)$ and hence $4 \vert (1+b)$. Hence, we get that $b=3$.

Therefore, $$-5 \equiv 3 \pmod{4}$$

To interpret $b \pmod{a}$ as a distance, in the sense you mean, it is the distance of $b$ from the largest multiple of $a$ no larger than $b$ i.e. the largest multiple of $a$ that falls to the left of $b$ or $b$ itself on the real number line. I have tried to illustrate this with couple of diagrams below.

The first picture indicates $b\pmod{a}$ when $b$ falls between $4a$ and $5a$.

The second picture indicates $b\pmod{a}$ when $b$ falls between $-3a$ and $-2a$.

-
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 65, "mathjax_display_tex": 6, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 5, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "github"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.9497672915458679, "perplexity_flag": "head"}