Find a formula for the integer with smallest absolute value that is congruent to an integer $a \pmod{m}$, where $m \in \mathbb{Z}$?
There is answer to this question here, but I will discuss a different solution.
Details:
- $x \equiv y \pmod{m}$ is by definition equivalent to $m|(x−y)$.
- $x \equiv y \pmod{m}$ , $x,y\in \mathbb{Z}, m \in \mathbb{Z^+}$ iff $a \pmod {m} = b \pmod{m}$.
- if $a | b$ and $a|c$, then $a|(b+c)$.
- if $a | b$ and $b|c$, then $a|c$.
- if $a | b$ then $a|bc$, for all integers $c$.
Attempt: I found a solution to this questions as follows:
Use the division algorithm to express $a = km + b$ with $0 \le b \lt m$. Let $s$ denote the integer with smallest absolute value that is congruent to $a \pmod{m}$.
Then $\mid s \mid = min \{b, m − b\}, s = b − \lfloor \frac{2b}{m} \rfloor$; or equivalently $s = min \{ x − \lfloor \frac{x}{m} \rfloor\cdot m , \lceil \frac{x}{m} \rceil \cdot m - x \} $.
Justification of these formulas:
- From the division algorithm and definitions of modular arithmetic we have that $b$ and $b − m$ are two integers congruent to a modulo $m$. And thus, the absolute value of s is the minimum of $\mid b \mid = b$ and $\mid b − m \mid = m − b$
- If $2b < m$, then $s = b$ and $b2b/mc = 0$. If $2b > m$, then $m − b \lt b$ and so $s = b − m$; note that in this case $\lfloor \frac{2b}{m} \rfloor = 1 $. Finally, if $2b = m$ then $m$ is even and $m − b = b$. With this, we conclude that in any of the three cases, the formula $s = b − m \lfloor \frac{2b}{m} \rfloor$ gives the right answer.
- If $b = 0$, then $\lfloor x/m \rfloor = k = \lceil \frac{x}{m} \rceil$ and $s = 0$. If $b \ne 0$, then $\lfloor \frac{x}{m} \rfloor = k$ and $\lceil \frac{x}{m} \rceil = k + 1$. It follows that $b = x − \lfloor \frac{x}{m} \rfloor$ and $b − m = \lceil \frac{x}{m} \rceil \cdot m - x$; which are the two possible values of $s$.
Questions:
- I am not sure about those properties mentioned in the solution based on details I provided above: "From the division algorithm and definitions of modular arithmetic we have that $b$ and $b − m$ are two integers congruent to a modulo $m$. And thus, the absolute value of s is the minimum of $\mid b \mid = b$ and $\mid b − m \mid = m − b$". How if $ a = km + b$, then $b$ and $b-m$.
- Why $b-m$ is there in $min \{b, m − b\}$?
- I am not sure how $2b \lt m$ was brought up in the solution and where it came from?
- Why $\lfloor \frac{2b}{m} \rfloor = 1 $ in the justification in case $2b \gt m$?
- How that in any of the three cases, the formula $s = b − m \lfloor \frac{2b}{m} \rfloor$ gives the right answer.
Note: Comments about solution being invalid refers to this one before I edited it to the current attempt above.
Solution: Thanks to the people commented on this question. Based on the division algorithm, we can find out that $r$ is the residue of $a \pmod{m}$ iff $a \equiv r \pmod{m}$, then $a = m\cdot k +r$. We can see that this $r$ is the residue of $a \pmod{m}$. To find the formula for the integer with smallest absolute value that is congruent to $a \pmod{m}$ is as follows,
$$ \{-\lceil \frac{m}{2} \rceil, \cdots, -1, 0, 1, \cdots, \lceil \frac{m}{2}\rceil\}$$
Omit one of $\{ -\lceil \frac{m}{2} \rceil, \lceil \frac{m}{2} \rceil \}$ in case $ \lceil \frac{m}{2} \rceil\ $ is even.