Some identities involving integer division

I have worked out quite some identities involving integer division while writing other articles, and I decided to write them down for reference.

The variables mm, nn, dd are assumed to be integers (with d0d \neq 0) throughout this article, whereas xx is a real variable.

Rounding up and down

When we divide unsigned integers n and d, the result gets rounded down. That is, n / d evaluates to nd\lfloor \frac{n}{d} \rfloor. If we want to round up the quotient, we can do it as (n + d - 1) / d. Here, we are using the following identity:
n+d1d=nd \lfloor \frac{n + d - 1}{d} \rfloor = \lceil \frac{n}{d} \rceil

The relation between the floor and the ceil function is that
x=x \lfloor - x \rfloor = - \lceil x \rceil

Rounding to multiples of dd

To round down an integer nn to the nearest multiple of dd, we can use the expression dndd \lfloor \frac{n}{d} \rfloor. Alternatively, we can use modular arithmetic to write this expression as
nmodd(n) n - \text{mod}_d(n)

Likewise, we can round up an integer nn to the nearest multiple of dd with the expression dndd \lceil \frac{n}{d} \rceil. With modular arithmetic we can write this as
n+modd(n) n + \text{mod}_d(-n)

Getting rid of floor/ceil

Sometimes, it is useful to get rid of the floor or ceiling function from an expression. From the results in the last section we can derive the following identities which allow you to do this.

For the floor function, we have:
nd=nmodd(n)d \lfloor \frac{n}{d} \rfloor = \frac{n - \text{mod}_d(n)}{d}

And for the ceil function:
nd=n+modd(n)d \lceil \frac{n}{d} \rceil = \frac{n + \text{mod}_d(-n)}{d}

Sum identity

I write qn=ndq_n = \lfloor \frac{n}{d} \rfloor, pn=modd(n)p_n = \text{mod}_d(n), so that we have n=dqn+pnn = d q_n + p_n.

By working out m+nd=(dqm+pm)+(dqn+pn)d\lfloor \frac{m + n}{d} \rfloor = \lfloor \frac{(dq_m + p_m) + (dq_n + p_n)}{d} \rfloor we can prove that
qn+m=qn+qm+pn+pmd q_{n + m} = q_n + q_m + \lfloor \frac{p_n + p_m}{d} \rfloor

Likewise, we can write sn=nds_n = \lceil \frac{n}{d} \rceil, rn=modd(n)r_n = \text{mod}_d(-n), so that n=dsnrnn = d s_n - r_{-n}.

Then, we can work out m+nd=(dsmrm)+(dsnrn)d\lceil \frac{m + n}{d} \rceil = \lceil \frac{(d s_m - r_m) + (d s_n - r_n)}{d} \rceil and get the identity
sn+m=sn+smrn+rmd s_{n + m} = s_n + s_m - \lfloor \frac{r_n + r_m}{d} \rfloor

Product identity

Again, I write qn=ndq_n = \lfloor \frac{n}{d} \rfloor, pn=modd(n)p_n = \text{mod}_d(n), so that we have n=dqn+pnn = d q_n + p_n.

By working out and simplifying the expression mnd=(dqm+pm)(dqn+pn)d\lfloor \frac{mn}{d} \rfloor = \lfloor \frac{(dq_m + p_m)(dq_n + p_n)}{d} \rfloor we can prove that
qnm=dqmqn+pmqn+qmpn+pnpmd q_{nm} = d q_m q_n + p_m q_n + q_m p_n + \lfloor \frac{p_n p_m}{d} \rfloor

Again writing sn=nds_n = \lceil \frac{n}{d} \rceil, rn=modd(n)r_n = \text{mod}_d(-n), we have n=dsnrnn = d s_n - r_{-n}.

Now we can work out mnd=(dsm+rm)(dsn+rn)d\lceil \frac{mn}{d} \rceil = \lceil \frac{(d s_m + r_m)(d s_n + r_n)}{d} \rceil and get the identity
snm=dsmsn(rmsn+smrn)+rnrmd s_{nm} = d s_m s_n - (r_m s_n + s_m r_n) + \lceil \frac{r_n r_m}{d} \rceil