0
$\begingroup$

I'm looking for a formula to easily compute: $$ \left\lceil \frac{x+1}{2} \right\rceil $$ The formula shouldn't use any floor, ceil or round function. I'm looking for something "simple".

$\endgroup$
  • $\begingroup$ Which one? Floor or ceiling? $\endgroup$ – Ty. Jun 11 at 19:57
  • 2
    $\begingroup$ What's wrong with $\operatorname{ceil}$? $\endgroup$ – Jair Taylor Jun 11 at 19:57
  • $\begingroup$ @Ty. ceil. Thank you for pointing out the mistake. $\endgroup$ – Jay Jay Jun 11 at 19:58
  • $\begingroup$ @JayJay does a summation count as "simple"? $\endgroup$ – Ty. Jun 11 at 19:58
  • 1
    $\begingroup$ Why is that expression itself a simple formula. Not sure how to make it simpler. $\endgroup$ – fleablood Jun 11 at 19:59
1
$\begingroup$

No closed-form expression with $+,-,\times,\div$ can "emulate" the ceiling function (in particular because these operators are continuous; all they allow are rational fractions). With these basic operators, you would need an expression of infinite size.

Periodic functions and their inverses, like

$$\frac1\pi\arctan(\tan(\pi x))$$ give you access to the fractional part, from which you can build the floor/ceiling. But this is by no means "simple".

The answer is essentially no way.

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ "No closed-form expression with +,−,×,÷ can "emulate" the ceiling function" Okay. Then what I was looking for isn't possible. I think this will be the closer to a "correct solution". Thank you. $\endgroup$ – Jay Jay Jun 11 at 20:06
1
$\begingroup$

If $\frac{x+1}{2}$ isn't an integer, then $$\left\lceil \frac{x+1}{2} \right\rceil=\left\lfloor \frac{x+1}{2} \right\rfloor+1$$ and $$(x+1) \pmod{2}=x+1-2\cdot\left\lfloor \frac{x+1}{2} \right\rfloor$$ $$ \iff \left\lfloor \frac{x+1}{2} \right\rfloor=\frac{(x+1)-((x+1) \pmod{2})}{2}$$ $$\implies \left\lceil \frac{x+1}{2} \right\rceil=\frac{(x+1)-((x+1) \pmod{2})}{2}+1$$ Otherwise $\left\lceil \frac{x+1}{2} \right\rceil=\frac{x+1}{2}$

| cite | improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ What's the closed form of $x \pmod 2$? $\endgroup$ – fleablood Jun 11 at 20:21
  • 1
    $\begingroup$ @AnasA.Ibrahim I appreaciate your answer and, even with the mistake, your result was useful in a certain way. Thanks for replying. :) $\endgroup$ – Jay Jay Jun 11 at 20:21
  • $\begingroup$ Stop denying other's comments that point at your gross mistakes. Your other answer is not unprecedented, this question was asked and answered zillion times. $\endgroup$ – Yves Daoust Jun 11 at 20:22
  • $\begingroup$ @JayJay you're welcome :) $\endgroup$ – Anas A. Ibrahim Jun 11 at 20:23
  • 1
    $\begingroup$ By the way, $\lceil\frac{x+1}2\rceil=\lfloor\frac{x+1}2\rfloor+1$ is wrong ! This is not your day. $\endgroup$ – Yves Daoust Jun 11 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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