2

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.

Jdeep
  • 303
  • https://math.stackexchange.com/a/975792/112944 reads like an answer to me if you substitute $x$, $y$ and $p$ in their appropriate places. – d125q Oct 06 '20 at 17:08
  • Note that multiples of same number form an arithmetic progression. – cosmo5 Oct 06 '20 at 17:17

1 Answers1

2

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

  • 1
    So I suppose $(\frac{p_ {last} - p_{first}}{p}) $/p will give me the required answer right? – Jdeep Oct 06 '20 at 17:13
  • Check it on an example - there is an off-by-one error in there. In fact, try out these formulas on several examples, both to see if they are correct and to understand why they work. – Johannes Kloos Oct 06 '20 at 17:18
  • @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 ? – Jdeep Oct 06 '20 at 18:18
  • 1
    @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. – Johannes Kloos Oct 06 '20 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? – Jdeep Oct 08 '20 at 11:49
  • There will be an edge case if $p$ divides $x$ in this case - try it out! – Johannes Kloos Oct 08 '20 at 12:06
  • @JohannesKloos , Oh I see. In that case , the multiples will be inclusive of x. – Jdeep Oct 09 '20 at 04:20