Skip to main content

Square Root - Newton's method

Definition​

Newton's method is an iterative algorithm used for finding successively better approximations to the roots of a real-valued function. In the context of finding the square root of a number, Newton's method can be applied iteratively to converge upon the square root

Practice​

square_root(number, tolerance):
// Initialize the initial guess
guess = number / 2

// Iterate until convergence
while true:
// Calculate the next guess
next_guess = (guess + (number / guess)) / 2

// Check if the tolerance is met
if abs(next_guess - guess) < tolerance:
return next_guess

// Update the current guess
guess = next_guess