The formula given in the title is used for approximating the roots of non-perfect square numbers.

$${\sqrt x}=\frac{x + y}{2\sqrt y}$$

Here, 'x' is the number you want the root for and 'y' is the nearest perfect square.

For example:

  • If x is 79, then y would come out to be 81
  • if x is 118, then y would come out to be 121

What I wish to find out is, how much the roots found using this formula differ from the actual roots.

For example:

  • Taking x = 79 and y = 81. Substituting that into equation we get ${\sqrt x}$ = 8.888. Which is exactly the value you get using a calculator 8.888
  • Taking x =118 and y = 121. Substituiting that into the equation we get ${\sqrt x}$ = 10.863. Which differs about 0.001 from the value you get using a calculator which is 10.862

My problem is, I wish to plot a graph which would take the root on the x-axis and plot the difference on the y-axis . I do not know of any program which can find the nearest perfect square as well as plot the graph.

manually finding the values and plotting them tells me the graph gives a saw tooth pattern where the highest difference exponentially decays as the values of x increase.

I tried doing this in desmos but as you could guess there is no way to find the 'y' for the different values of 'x'.

P.S. I have no idea what tags to use in this so I am sorry for any mistake


1 Answer 1


In gnuplot, you can define function with tests. Recall that the function int returns the integer part of its argument, truncated toward zero. Then, y(x) should be either int(sqrt(x))**2 or (int(sqrt(x))+1)**2, whichever is the closest (in gnuplot, x**2 means x*x). Then you could define

 y(x) = x- int(sqrt(x))**2 < (int(sqrt(x))+1)**2 - x ? int(sqrt(x))**2 : (int(sqrt(x))+1)**2

Then your plot can be obtained with

plot [0:100] ( x + y(x) ) / ( 2 * sqrt(y(x)) ) - sqrt(x)

Second method, which should work with all plotter program. Let $n=\lfloor\sqrt x\rfloor$ the integer part of $\sqrt x$. Then $y$ should be either $n^2$ or $(n+1)^2$. It should be $(n+1)^2$ if and only if $$ x-n^2 \ge (n+1)^2-x$$ after rearranging this, this is equivalent to $$(n+\tfrac12)^2\le x-\tfrac14$$ I am going to assume for simplicity that $x>\tfrac14$. For the general case, you could replace in what follows $x-\tfrac14$ by $\max(0,x-\tfrac14)$. Then the condition is equivalent to $$ n+1 \le \sqrt{x-\tfrac14}+\tfrac12$$ Then, clearly, $$ y(x)= \Big\lfloor \sqrt{x-\tfrac14}+\tfrac12\Big\rfloor^2 $$ or, as a gnuplot command,

y(x)= ( int( sqrt(x-.25)+.5)**2
  • $\begingroup$ I just came to know about 'gnuplot' so correct me if I am wrong, ` y(x) = x- int(sqrt(x))**2 < (int(sqrt(x))+1)**2 - x ? int(sqrt(x))**2 : (int(sqrt(x))+1)**2` is used to find the nearest perfect square? and y(x)= ( int( sqrt(x-.25)+.5)**2 does the same thing? except the second second has a minimum value of x = 1/4 right? $\endgroup$ Mar 31, 2023 at 8:19
  • $\begingroup$ Yes. Once y(x) is defined, plotting the graph is easy. For the first version, the syntax used is y(x)= [condition] ? [if true] : [if false], as in C. $\endgroup$ Apr 1, 2023 at 18:20
  • $\begingroup$ I see, thank you. I am sorry I can not upvote your post due to reputation limitations, but regardless I appreciate the answer. $\endgroup$ Apr 3, 2023 at 11:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .