Of course, the LotusScript language includes the SQR function for calculating the square root of a given number. The following algorithm is easy to understand and achieves good accuracy after just a few iterations.
The Heron Method (Babylonian Method)
This code calculates the square root of any positive number without using the built-in
Sqrt function. Instead, it uses the Heron method (also known as the Babylonian method) – an iterative algorithm that is over 2,000 years old.The Code
Function MySquareRoot(number As Double) As Double ' Square root using the Heron method (Babylonian method) ' without using the Sqrt function Dim estimate As Double Dim previous As Double Dim precision As Double Dim iteration As Integer If number < 0 Then Error 5 ' Invalid input End If If number = 0 Then MySquareRoot = 0 Exit Function End If precision = 0.0000000001 ' 10 decimal places estimate = number / 2 ' Starting value: half the input number iteration = 0 Do previous = estimate ' Heron formula: new value = (old value + number / old value) / 2 estimate = (estimate + number / estimate) / 2 iteration = iteration + 1 Print "Iteration " & iteration & ": " & Format$(estimate, "0.0000000000") Loop Until Abs(estimate - previous) < precision MySquareRoot = estimate End Function
Call Example
Sub Click(Source As Button) Dim number As Double Dim result As Double number = 50 Print "Calculating square root of " & number & " ..." Print "---" result = MySquareRoot(number) Print "---" Print "Result: " & Format$(result, "0.0000000000") Print "Check: " & Format$(result * result, "0.0000000000") End Sub
How It Works in Detail
The Heron method is based on a simple idea:
1. Choose a Starting Value
We take
number / 2 as the first estimate. The starting value does not need to be particularly good; the method converges extremely quickly regardless.2. Apply the Iteration Formula
In each round, we calculate:
Why does this work?
- If
x_oldis too large, thennumber / x_oldis too small – and vice versa.
- The average of both therefore always lies closer to the true root.
- Mathematically, this is Newton's method applied to the equation .
3. Termination Condition
As soon as two consecutive estimates differ by less than the chosen precision (
0.0000000001), the loop terminates.Example: Square Root of 50
The true square root of 50 is 7.0710678118…
Iteration | Estimate | Error |
1 | 26.0000000000 | ~18.93 |
2 | 13.9615384615 | ~6.89 |
3 | 8.7731197691 | ~1.70 |
4 | 7.2376726427 | ~0.17 |
5 | 7.0730031544 | ~0.002 |
6 | 7.0710680509 | ~0.0000002 |
7 | 7.0710678119 | ~0.0000000001 |
After only 7 iterations, the method has calculated the root to 10 decimal places.
Key Properties
- Quadratic Convergence – The accuracy doubles with each step. Even with a poor starting value, rarely more than 10 iterations are needed.
- Robustness – The method works for any positive number, no matter how large or small.
- Historical – The method is over 2,000 years old and was already used by the Babylonians – hence the name.