Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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.

share|improve this question
Use the ceil function (not floor). –  dtldarek Mar 14 '12 at 16:16
add comment

1 Answer

up vote 5 down vote accepted

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

share|improve this answer
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). –  MJD Mar 14 '12 at 16:25
I used floor because that's the default python uses when doing integer division. The answer is incredibly simple using ceiling, though. Thanks! –  sgtFloyd Mar 14 '12 at 16:32
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.