How to find the closest integer to a given number $y \in \mathbb{R}$ that has a perfect root $x \in \mathbb{Z}: \sqrt x = k \in \mathbb{Z}$?

48 Views Asked by At

Background Theory

An approximation of a number $x_0$ can be made using using derivatives, based on the formula $(1)$

$$f(x_0 + \Delta x) \approx f(x_0) + f'(x_0)*\Delta x\quad (1)$$

For example we can approximate $\sqrt24$ by choosing $f(x) = \sqrt x$ and plugging in the equation $x_0 = 25$ and $\Delta x = -1 $ i.e:

$$f(25 - 1) \approx f(25) + f'(25)*(-1) \iff \sqrt24 \approx4.9$$

Note: We chose $25$ because it is the closest integer to 24 that has a perfect square root. Therefore, by doing this, we minimize the approximation error.


The Question

So, I am wondering:

How can one find the closest integer $x$ to any given $y \in \mathbb{R}: \sqrt x = k \in \mathbb{Z}$?

Is there a clever mathematical way to do this or should one use a heuristic algorithm?

1

There are 1 best solutions below

1
On BEST ANSWER

It's the same problem as finding the integer closest to $\sqrt y$, so it amounts to finding a good approximation to $\sqrt y$ (which isn't going to help you, since finding a good approximation to $\sqrt y$ is what you want to do in the first place. Nevertheless,...) one way to do this is to start with any approximation $y_0$ to $\sqrt y$, even something pathetic like $y_0=1$, and then improve it by running a few rounds of Newton's Method. You know Newton's Method?