1
$\begingroup$

I'm looking for an equation to calculate the lowest multiple of any value $n$ greater than or equal to a given threshold, $y$. I've come up with a solution that works, but it's rather ugly and not incredibly efficient. I'll be using this within software, so efficiency matters to some degree.

Here's what I've come up with:
$x$ = $y$ (mod $n$) and $1$ x = 0 if y (mod n) = 0, otherwise 1
$m$ = $x(n(floor(y/n)+1)-y)+y$

This only needs to work for positive values of $y$ and $n$. Any suggestions would be greatly appreciated.

  • 0
    Use the ceil function (not floor).2012-03-14

1 Answers 1

5

It's $n\cdot\lceil y/n \rceil$.

  • 0
    If for some reason your programming system has only floor ($\lfloor x\rfloor$) and not ceiling ($\lceil x \rceil$) you can synthesize ceiling as `if (x == floor(x)) then x else 1+floor(x)`.2012-03-14
  • 0
    I used floor because that's the default python uses when doing integer division. The answer is incredibly simple using ceiling, though. Thanks!2012-03-14