Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

$$L = \left\lceil \frac{\sqrt{v-4 \times N}-1}{4} \right\rceil$$

This is a line in my program but I cannot get ceil() to work in GMP, so I'd like to approach this mathematically and just rewrite it so it doesn't need ceil().

share|cite|improve this question
What are $v$ and $N$? Fractions, integers? – Antonio Vargas Nov 28 '12 at 0:14
@AntonioVargas Integers – KaliMa Nov 28 '12 at 0:36
Does floor work for you? – hardmath Nov 28 '12 at 0:58
@hardmath Yes, floor is fine -- ceil is the only problem – KaliMa Nov 28 '12 at 1:13
ceil(x) = - floor(-x). – coffeemath Nov 28 '12 at 2:42
up vote 2 down vote accepted

You can use the floor with the difference of the desired quantity and a sufficiently large integer. In particular here we know:

$$ v > \frac{\sqrt{v-4 \times N}-1}{4} $$


$$ L = \left\lceil \frac{\sqrt{v-4 \times N}-1}{4} \right\rceil = v - \left\lfloor v - \frac{\sqrt{v-4 \times N}-1}{4} \right\rfloor $$

'Nuff said? There are other ways to do it, but you have $v$ on hand and this is probably the fewest additional operations (and avoids any case logic). Certainly worth a comment in the code so you don't have a long moment of confusion months down the road...

share|cite|improve this answer

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.