How to find the square root of a number without using library function in ALGOL?
To conduct square root calculations without using the library function in ALGOL, an iteration algorithm, such as the Babylonian method (also known as Heron's method), can be applied. This algorithm is one of the simplest and most efficient numerical techniques to produce square root approximations.
This is how we do it:
Step 1: Set an initial guess value.
Begin by determining an initial estimate of the square root. This value can be any non-negative number. A common starting guess is half the given number for which we are trying to find the square root.
REAL x, guess;
guess := x / 2;
Step 2: Improve the guess.
Subsequently, use this guess to perform the primary computation. The next guess is calculated as the arithmetic mean of the current guess and the quotient of the number (for which we are finding the square root) and the current guess.
guess := (guess + (x / guess)) / 2;
Step 3: Check for convergence.
Next, the algorithm needs to check if the guess has converged, meaning that the guess is close enough to the actual square root value. We do this by computing the square of the guess and comparing it with the original number. If the absolute difference is less than a set threshold (this can be a very small number like 0.00001), then the guess has converged.
if ABS(guess * guess - x) < 0.00001 then goto step 5;
Step 4: If the guess has not yet converged, return to Step 2.
The pseudocode looks like this:
goto step 2;
Step 5: Once convergence has been reached, the algorithm stops, and the final guess is the square root of the number.
PRINT('The square root of ', x, ' is approximately ', guess);
This is the entire process to find the square root of a number without using the library function in ALGOL. Despite the absence of a pre-defined function, the above steps and methods are useful in enabling calculations to be performed directly. Note that the precision of the result is influenced by both the initial guess and the threshold of convergence.