# How to calculate `sqrt()` Question:
How do you calculate `sqrt()`?

Of course the best way to compute square roots is to use`Math.sqrt()`,but I assume your question is academic. I’m not sure of the bestway to find square roots, but historically, Newton’s Method is themost significant.

Newton’s Method tells us how to approximate an x-interceptof a differentiable curve described by the equation y = f(x). Howdoes this help us to find the square root of 42? Well, thex-intercepts of the parabola y = x^2 – 42 occur at sqrt(42) and-sqrt(42).

The x-intercept of a curve y = f(x) occurs at a point x = a such thatf(a) = 0. So, Newton’s Method iterates a sequence of progressivelybetter guesses until f(guess) is acceptably close to 0:

`guess = 1;   while (delta < |f(guess)|)  // delta small      guess = improve(guess);   return guess;`
How do we improve a bad guess?Newton’s idea is this:
Draw a line tangent to the curve at the point (guess, f(guess)).
Using calculus, it’s pretty easy to get the equation for this line:
y – f(guess) = slope * (x – guess)
where slope = f'(guess) = the derivative of f at x = guess.

Newton reasoned that this line crudely approximates the curve y = f(x)near the point of tangency, and therefore the x-intercept of this linecrudely approximates the x-intercept of y = f(x).

It’s easy to calculate the x-intercept of this line. Just set y = 0 andsolve for x:

x = guess – f(guess)/f'(guess)
Hence:
improve(guess) = guess – f(guess)/f'(guess)
>From an implementation standpoint, the only complication is computingf'(x). Again we dust off our calculus book, where we discover
f'(x) = limit (f(x + delta) – f(x))/delta as delta tends to 0
Dropping the limit and choosing delta to be suitably small shouldgive us an acceptable approximation of f'(x).

Here’s a Java implementation with a crude GUI. It can be used tofind the n-th root of any real number a where 0 <= a and 0 <= n.

`import java.awt.*;// A system for approximating n-th root of aclass Solver {   private int n;   private double a;   private static double delta = 0.00000001; // controls accuracy   private static double initGuess = 1;   // constructs a solver for x^n – a   public Solver(int x, double y) { n = x; a = y; }   // your basic iterator for the improve method   public double solve() {      double guess = initGuess;      while(!goodEnough(guess))         guess = improve(guess);      return guess;   }   // f(x) = x^n – a = the function we’re solving   private double f(double x) {      return Math.pow(x, n) – a;   }   // df(x) = f'(x), approximately   private double df(double x) {      return (f(x + delta) – f(x))/delta;   }   // 0 <= |f(guess)| <= delta?   private boolean goodEnough(double guess) {      return (Math.abs(f(guess)) <= delta);   }   // Newton's Method   private double improve(double guess) {      return guess - f(guess)/df(guess);   }}// a primitive GUIpublic class Main extends Frame {   private TextField root;   private TextField base;   private TextField result;   public Main() {      setTitle("Root Tester");      setLayout(new FlowLayout());      root = new TextField("root", 15);      base = new TextField("base", 15);      result = new TextField("result", 15);      add(root);      add(base);      add(result);   }   public boolean action(Event e, Object o) {      if (e.target == base) {         double d = Double.valueOf(base.getText()).doubleValue();         int i = Integer.valueOf(root.getText()).intValue();         if (0 <= i && 0 <= d) {            Solver s = new Solver(i, d); // make a solver for x^i - d            result.setText("+/- " + String.valueOf(s.solve()));         }         else result.setText("0"); // for now      }      repaint();      return true;   }   public static void main(String args[]) {      Frame m = new Main();      m.resize(300, 200);      m.show();   }} `

Share the Post: ### The Role of Call Data: How Call Tracking Can Improve Customer Experience

Who would argue that experiences reign supreme? But not just any experiences — the ones that occur when customers interact with your business. It’s these real-life interactions that have the ### Data Observability Explained

Data is the lifeblood of any successful business, as it is the driving force behind critical decision-making, insight generation, and strategic development. However, due to its intricate nature, ensuring the ### Logitech G502 Software: Optimize and Customize Your Gear

One of the most significant surges of the 21st century is gaming. Gaming is more popular than ever before thanks to innovative new consoles, high-tech PC setups, mobile gaming improvements, ### Revolutionizing Search: A Glimpse Into Google’s Generative Experience

Google is revolutionizing the search experience as we know it with its latest generative experience. No longer will you be bound by the limitations of traditional keyword searching. Now, you ### GM Creates Open Source uProtocol and Invites Automakers to Adopt It: Revolutionizing Automotive Software Development.

General Motors (GM) recently announced its entry into the Eclipse Foundation. The Eclipse Foundation is a prominent open-source software foundation. In addition, GMC announced its contribution of “uProtocol” to facilitate ### What We Should Expect from Cell Phone Tech in the Near Future

The earliest cell phones included boxy designs full of buttons and antennas, and they only made calls. Needless to say, we’ve come a long way from those classic brick phones ### The Digital Panopticon: Is Big Brother Always Watching Us Online?

In the age of digital transformation, the internet has become a ubiquitous part of our lives. From socializing, shopping, and learning to more sensitive activities such as banking and healthcare, ### Embracing Change: How AI Is Revolutionizing the Developer’s Role

The world of software development is changing drastically with the introduction of Artificial Intelligence and Machine Learning technologies. In the past, software developers were in charge of the entire development