Sign up ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

Is there a way to determine this without brute force?

For example


range $= [17,24]$

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

share|cite|improve this question

3 Answers 3

up vote 3 down vote accepted

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

share|cite|improve this answer
What do you do if Z/X and/or Y/X don't divide cleanly? – Aristides Sep 20 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 at 12:00

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

share|cite|improve this answer
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

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.