sqrt-newton
Calculate square roots using the Newton-Raphson method.
For univariate differentiable functions between real numbers, the Newton-Raphson method is an iterative root finding method that works by repeatedly finding the intersection between the tangent of a function at the current guess, and the f(x) == 0
line.
Having our current guess n0
, we can calculate n1 = n0 - f(n0)/diff(f)(n0)
.
That means that we can easily devise calculating a square root using the following method:
- Take equation
x == sqrt(n)
- Square both sides
x*x == n
(this implies that our algorithm only works for a non-negative n) - Subtract n from both sides
x*x - n == 0
. This is ourf(x)
- Get derivative
2*x
- Get recurrence relation
x1 = x0 - (x0*x0 - n)/(2*x0)
- Iterate until satisfied with precision