2
$\begingroup$

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.

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

1 Answer 1

2
$\begingroup$

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

$\endgroup$
7
  • 1
    $\begingroup$ So I suppose $(\frac{p_ {last} - p_{first}}{p}) $/p will give me the required answer right? $\endgroup$
    – Jdeep
    Oct 6, 2020 at 17:13
  • $\begingroup$ 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. $\endgroup$ Oct 6, 2020 at 17:18
  • $\begingroup$ @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 ? $\endgroup$
    – Jdeep
    Oct 6, 2020 at 18:18
  • 1
    $\begingroup$ @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. $\endgroup$ Oct 6, 2020 at 18:24
  • $\begingroup$ 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? $\endgroup$
    – Jdeep
    Oct 8, 2020 at 11:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .