Using the Newton-Raphson Method to solve square roots

Gideon Weiss
3 min readOct 28, 2022

When programming, getting the square root of a number is a very simple process, just parse the number to relevant built-in function and you the answer, but have you ever tried to get the square root of a number without using the built-in functions?

One way new coders might attempt this is by brute forcing an answer by trial and error:

This however is a very bad solution! It is slow and will only work if the number is a perfect square.

There is a better way of course! Using the Newton-Raphson Method.

The Newton-Raphson Method allows you to determine the root of a function. Unfortunately, x = 4 is not a function we can use to determine the square roots of 4. Therefore we have to turn it into a function. The easiest way to do this is to create a quadratic equation out of the value:

This function on its own will not work however, as its root will only touch the x-axis. We need the roots (x-intercepts) to be equal to the square root of the number.

f(x) = x² graphed on Desmos

Using some simple maths we can determine that we can create a quadratic equation with the roots x = -2 and x = 2:

Graphing this function we can see that the roots (x-intercepts) of the function are the possible answers to the square root. In this case we will only be working with the positive value.

If you have a keen eye, you may have also realised you can just factorise the function and then determine the x-intercepts and thus the square root, however when we are trying to square root a number that is not a perfect square this method becomes a lot trickier as you cannot easily determine a function with the roots equal to the square root of the number.

If you have noticed, this gives us a general formula,

with xₙ being the current value of x and xᵢ being the original value (in this case 4). Using this formula we can determine a function for any number to determine the square roots.

We can plug the function that we made into the iterative Newton-Raphson Method formula:

If you use this formula over and over again, each time setting the answer to xₙ and repeating you will get closer and closer to the square root:

After 5 iterations we get to x = 2. If we started with xₙ = -4 we would have resulted in x = -2, the other root of the function x²-4!

So what is going on here?

Each the x-intercept of each tangent calculated will tend towards the root of the function:

Newton-Raphson Method visualized using Desmos

You can see that the x-intercepts of the tangents are the same as the values of xₙ as we continue calculating using the formula we derived.

Using this method we can calculate the square root of any number programmatically without using the built-in functions.

Code for Newton-Raphson Method

We have to check to see if the given number to square root is 0 otherwise we will be dividing by 0. In this example I programmed it so the formula iterated 20 times which should be enough to ensure the first few decimal places of the root calculated are accurate for most numbers. If 20 is not enough you can just increase the number.

This is a really nifty and interesting way of calculating square roots of any number easily.

--

--