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

703 Views Asked by At

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.

1

There are 1 best solutions below

7
On BEST ANSWER

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