# 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$.

-
What do you do if Z/X and/or Y/X don't divide cleanly? – Aristides Sep 20 '15 at 21:52
That's why we use the floor $\lfloor\ \rfloor$ and ceiling $\lceil\ \rceil$ functions: if $Z/X$ (or $Y/X$) isn't an integer, we round down (or up) to the nearest. – Owen Biesel Oct 5 '15 at 12:00

Isnt this similar to $\lfloor (Z - Y) / 3\rfloor$

http://maths-on-line.blogspot.in/

-
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

An easy way to find out the number of multiples of X in the range [Y,Z] is: ($\lfloor$Z/X$\rfloor$ - $\lfloor$(Y-1)/X$\rfloor$)

The (Y-1) is for the case where Y is divisible by X

In your example, the answer would be computed as: ($\lfloor$24/3$\rfloor$ - $\lfloor$(17-1)/3$\rfloor$) = 8 -$\lfloor$5.666...$\rfloor$ = 3

-