Tuesday, July 28, 2015

Closest integer square

Given a real number $x\geq 0$, consider the following 2 functions: $f(x)$ is the closest square of an integer to $x$, and $g(x)$ is the integer closest to $\sqrt{x}$.
In other words, $f(x) = y$ where $y = m^2$ for an integer $m$ and $|x-y| \leq |x-n^2|$ for all integers $n$.  In case of a tie, i.e. $x$ (resp. $\sqrt{x}$) is midway between two successive squares (resp. integers), we define $f(x)$ (resp. $g(x)$) to be the smallest such square (resp. integer).

The question we like to ask is: is $g(x)^2 = f(x)$ true?  For arbitrary real numbers $x$, the answer is no.

The midpoint between two successive integers $a$ and $a+1$ is $a+\frac{1}{2}$ whose square is $a^2+a+\frac{1}{4}$.  On the other hand, the midpoint between two successive squares $a^2$ and $(a+1)^2$ is $a^2+a+\frac{1}{2}$.  It is interesting to note that the difference between the 2 midpoints is always $\frac{1}{4}$ regardless of what $a$ is (this is true even if $a$ is not an integer).
Graphically this is illustrated as:


This means that for $a^2+a+\frac{1}{4} < x < a^2+a+\frac{1}{2}$, $g(x) = \sqrt{f(x)}+1$.  For instance $2.375$ is closer to $1$ than to $4$, but $\sqrt{2.375} = 1.541...$ is closer to  $2$ than to $1$.

On the other hand if
$a^2 \leq x < a^2+a+\frac{1}{4}$ or $a^2+a+\frac{1}{2} < x \leq (a+1)^2$,
then $g(x)^2 = f(x)$.

This analysis also shows the following facts:

  • If $x$ is an integer, then evaluating $f(x)$ and $g(x)$ do not result in a tie, as the 2 midpoints above are not integers.
  • If $x$ is an integer, then $g(x)^2 = f(x)$ as the interval $[a^2+a+\frac{1}{4}, a^2+a+\frac{1}{2}]$ does not contain any integers.