TAOCP 1.2.4 Exercise 35

Let $x$ be a real number, and let $m$ and $n$ be integers with $n>0$.

Section 1.2.4: Integer Functions and Elementary Number Theory

Exercise 35. ▶ [M20] Given that $m$ and $n$ are integers and $n>0$, prove that

$$\left\lfloor \frac{x+m}{n} \right\rfloor = \left\lfloor \frac{\lfloor x \rfloor + m}{n} \right\rfloor$$

for all real $x$. (When $m=0$, we have an important special case.) Does an analogous result hold for the ceiling function?

Verified: yes
Solve time: 1m18s


Solution

Let $x$ be a real number, and let $m$ and $n$ be integers with $n>0$. Write

$x = \lfloor x \rfloor + {x}, \quad 0 \le {x} < 1,$

where ${x} = x \bmod 1$ is the fractional part of $x$. Then

$$ \frac{x+m}{n} = \frac{\lfloor x \rfloor + m}{n} + \frac{{x}}{n}. $$

Since $0 \le {x}/n < 1/n < 1$, the integer part of $\frac{x+m}{n}$ is equal to the integer part of $\frac{\lfloor x \rfloor + m}{n}$. That is,

$$ \left\lfloor \frac{x+m}{n} \right\rfloor = \left\lfloor \frac{\lfloor x \rfloor + m}{n} \right\rfloor. $$

This proves the desired equality.

When $m=0$, we obtain the important special case

$$ \left\lfloor \frac{x}{n} \right\rfloor = \left\lfloor \frac{\lfloor x \rfloor}{n} \right\rfloor, $$

which allows one to reduce floor computations of real numbers to floor computations of integers.

For the ceiling function, write

$$ \frac{x+m}{n} = \frac{\lfloor x \rfloor + m}{n} + \frac{{x}}{n}, \quad 0 \le {x}/n < 1. $$

If ${x} = 0$, then clearly $\lceil (x+m)/n \rceil = \lceil (\lfloor x \rfloor + m)/n \rceil$. However, if ${x} > 0$ and $(\lfloor x \rfloor + m)/n$ is an integer, then $\lceil (x+m)/n \rceil = (\lfloor x \rfloor + m)/n + 1$, while $\lceil (\lfloor x \rfloor + m)/n \rceil = (\lfloor x \rfloor + m)/n$. Hence the analogous equality fails in general for the ceiling function.

This completes the proof.

$$ \boxed{\left\lfloor \frac{x+m}{n} \right\rfloor = \left\lfloor \frac{\lfloor x \rfloor + m}{n} \right\rfloor, \quad \text{ceiling version false in general}} $$