# Is there any general algorithm of finding multiples of a given number in a given range?

Is there any way of finding multiples of a given number , say p , in the range (x , y) {exclusive of x and y}

I found this question . But the answers only address the example given by the OP and not as general algorithm.

• math.stackexchange.com/a/975792/112944 reads like an answer to me if you substitute $x$, $y$ and $p$ in their appropriate places. Oct 6, 2020 at 17:08
• Note that multiples of same number form an arithmetic progression. Oct 6, 2020 at 17:17

Here's a relatively simple solution: Let's assume $$p$$ is a natural number.
The first multiple of $$p$$ in $$(x,y)$$ will be $$p \cdot (\lfloor x/p \rfloor + 1)$$ and the last multiple will be $$p \cdot (\lceil y/p \rceil - 1)$$, where $$\lceil \cdot \rceil$$ stands for "round upwards" and $$\lfloor \cdot \rfloor$$ stands for "round downards".
• So I suppose $(\frac{p_ {last} - p_{first}}{p})$/p will give me the required answer right? Oct 6, 2020 at 17:13
• @JohannesKloos , Wait .Thats interesting. I wrote a script and that error factor is not an error factor but a required part of the answer. I have tested it on lots of cases and always $\frac{p_{last} - p_{first}}{p} + 1$ is the answer. From where is the +1 coming from ? Oct 6, 2020 at 18:18
• @NeoNØVÅ7 If you have a range of natural numbers from $a$ to $b$ (inclusive), that range will include $b-a+1$ numbers. This can be understood by seeing that $b-a$ counts all numbers $\le b$, but not $\le a$ - it does not count the start point. Oct 6, 2020 at 18:24
• One more question,. Rather than using $p \cdot (\lfloor x/p \rfloor + 1)$ , I think we can also use $p \cdot (\lceil x/p \rceil)$ . Can we? Or will there be any edge cases? Oct 8, 2020 at 11:49