# How do you find the number of multiples of a given range of numbers?

I know this sounds a bit stupid but this question always confounds me. Say that you are given a range of numbers like $20$-$300$. And it asks you to find how many multiples of $5$ are given in that range. How would you proceed? What would the answers be for inclusive and exclusive numbers?

$$20=5\times 4$$ and $$300=5\times 60$$. So it has from $$4$$th multiple to $$30$$th. Thus including $$20$$ and $$300$$ it has $$60-4+1=57$$ multiples. If suppose instead of $$300$$, you had $$x$$ on the end where $$x$$ is not a multiple of $$5$$ then you just take first multiple of $$5$$ when you start walking left on integer line from $$x$$, i.e. if $$x=304$$, you replace it by $$300$$ again. similarly for non multiple on beginning, but now walk right.

• I can't post this as a separate answer, but for anyone else tackling this problem, for the interval $[a, b]$ and the factor $f$, you walk left exactly $b \bmod f$ and right exactly $(-a) \bmod f$. So a single formula to solve this problem could be $\lfloor \frac{b - b\bmod f - a - (-a) \bmod f}{f} \rfloor + 1$ Sep 5, 2021 at 2:04

Hint Count all the numbers such that they end in either a $5$ or a $0$. For the numbers $0-30$ inclusive that is $7$ numbers.

This may be logically equivalent to Bhaskar Vashishth’s answer, but IMNSHO this is clearer.

1. Figure out how many multiples of $5$ there are between $0$ and $300$.  Hint: $\frac{300}5$ may have something to do with this.
2. Figure out how many multiples of $5$ there are between $0$ and $20$.
3. Subtract result 2 from result 1.
• Where did the 20 come from? Oct 16, 2014 at 0:42
• From the question: "Say that you are given a range of numbers like 20-300". Oct 16, 2014 at 18:30

Let the range given be $$[x,y]$$, and the number whose multiple are to be counted in that range to be $$z$$,Any multiple of z in range $$[x,y]$$ would be of form zk,where k is any whole number, hence $$x \leq zk \leq y$$,which reduces to

$$\dfrac xz \leq k \leq \dfrac yz$$

now you have got range of k,counting number of multiples of z is equivalent to counting number of integral values of k in this range,that is $$\left\lfloor \frac{y}{z} \right\rfloor - \left\lceil \frac{x}{z} \right\rceil +1$$

number of multiples of 5 in range [20,300] equals$$\left\lfloor \frac{300}{5} \right\rfloor - \left\lceil \frac{20}{5} \right\rceil +1$$ $$=60-4+1$$ $$=57$$ Note:If the range in (x,y] or [x,y) or (x,y) instead of [x,y], then you have to subtract unwanted mulitples as per the conditions after using above formula

• I've extended your answer below by providing all possibilities and extending multiplier to be non-integer Apr 8 at 0:58

In general one possible thing to do is the following:

rearrange the first and the last number (as in Bhaskar Vashishth's response) so that they are multiples of the number $n$ you want to count multiples of (5 in this case).

Then substract the two numbers, divide by $n$ and add one to get the inclusive answer

I will provide an example for illustration.

Question: Find the no. of multiples of 4 between 1 and 605, inclusive.

Solution Methodology: The formula can be put down as : (Greatest Multiple of the number in the Range - Smallest multiple of the number in the range ) divided by the number. To this result add 1 to account for inclusivity.

As an expression this can be written as: (GMIR-SMIR)/The Number)+1
Note: GMIR - Greatest Multiple In Range. SMIR- Smallest Multiple In Range.

For the illustration: Greatest multiple of 4 in the range ( 1 to 605) can be found out by dividing 605 by 4. You would get a quotient of 151 and remainder 1. 151 multiplied by 4 is 604. This is the greatest multiple of 4 inside the range.

Smallest multiple of the range can be found by dividing 1 by 4. Answer would be 0. It doesn't fit in the range. So check for the next multiple in the range, which is 4.

Now, use the formula (Greatest Multiple in Range - Smallest Multiple in Range) divided by the number which would be (604-4)/4 equalling 150.

Now add 1 to 150, equalling 151. (We add 1 to account for inclusivity of the extreme multiples).

This solution works for any range , inclusive. If not inclusive, you would have to tweak the results to remove the inclusive multiple.

I'd like to extend an answer with formulas for every possible situation.

I do following terminology here: $$x$$ is the minimal bound o the range, $$y$$ is maximum bound of the range, $$k$$ is multiplier, and we're looking for multiples with integer coefficients.

It's safe to assume, that if a number is divisible by $$k$$, then it's divisible by $$-k$$. And it's safe to assume that if there's $$n$$ multiplers in a range of $$k$$, that there's same amount of multiplers of $$-k$$. So for the rest of the post I'll assume that $$k > 0$$.

For closed range $$[x, y]$$ formula is as presented above $$\left\lfloor \frac{y}{k} \right\rfloor - \left\lceil \frac{x}{k} \right\rceil +1$$

For half-open range $$(x,y]$$ formula is $$\left\lceil \frac{y+k}{k} \right\rceil - \left\lceil \frac{x}{k} \right\rceil +1$$

For half-open range $$[x,y)$$ formula is $$\left\lfloor \frac{y}{k} \right\rfloor - \left\lfloor \frac{x+k}{k} \right\rfloor +1$$

For open range $$(x,y)$$ formula is $$\left\lceil \frac{y+k}{k} \right\rceil - \left\lfloor \frac{x+k}{k} \right\rfloor +1$$

Few notes here:

• this works with all real numbers $$k \neq 0$$. E.g. in a range $$[\sqrt 2 \leq \sqrt 2a \leq 2\sqrt 2$$, there's 2 multiples of $$\sqrt 2$$: $$\sqrt 2$$ and $$2\sqrt 2$$ (coefficients are $$1$$ and $$2$$ respectively).

• $$\frac {a + c}{c}$$ may have less computational error than $$\frac {a}{c} + 1$$ in edge cases for IEEE 754 calculations (basically almost all modern computers, cell phones and servers and most embedded devices), because addition has less error than division.