The answer gives the following code for computing floor(sqrt(x))
using just integers. Is it possible to use/modify it to return ceil(sqrt(x))
instead? Alternatively, what is the preferred way to calculate such value?
Edit: Thank you all so far and I apologise, I should have make it more explicit: I was hoping there is more "natural" way of doing this that using floor(sqrt(x))
, possibly plus one. The floor
version uses Newton's method to approach the root from above, I thought that maybe approaching it from below or similar would do the trick.
For example the answer even provides how to round to nearest integer: just input 4*x
to the algorithm.