# How many multiples of X lie in the arbitrary range [Y,Z]?

Is there a way to determine this without brute force?

For example

$X=3$

range $= [17,24]$

The multiples of $X$ in this range are $18$, $21$, and $24$, so $3$ multiples total.

-

The smallest integer $n_1$ such that $n_1X \geq Y$ is $n_1=\lceil Y/X \rceil$, and the largest integer $n_2$ such that $n_2X \leq Z$ is $n_2 = \lfloor Z/X\rfloor$. The number of integers between $n_1$ and $n_2$ inclusive is $\max\{n_2 - n_1 + 1,0\}$, so putting that together, we get $$\max\{\lfloor Z/X\rfloor - \lceil Y/X \rceil + 1, 0\}.$$
In your example, $X=3$, $Y=17$, and $Z=24$, so we have $$\max\{\lfloor 24/3\rfloor - \lceil 17/3 \rceil + 1, 0\} = \max\{8-6+1,0\} = 3$$ multiples of $3$ between $17$ and $24$.
Isnt this similar to $\lfloor (Z - Y) / 3\rfloor$
It is close, but you could be off by 1. Consider $Y=1,Z=10$, for example. Because the interval is closed, $\lfloor (Z - Y +1) / 3\rfloor$ is better, but still not always right. –  Ross Millikan Dec 8 '12 at 2:59