Babylonian Method is one of the easiest methods to calculate square root of non-negative number.
Algorithm:
IN non-negative real numbers: x, precision OUT square root of x 1. Set x to number / 10 and y to number / x. 2. Make x = (x + y) / 2 and y = number / x. 3. Check if x - y > precision. 4. If it is true go to step 2. 5. If not end algorithm and return x as square root of given number.
Sample Output:
IN 3 0.0000000001 OUT 1.7320508082191837
Iteration Number | x | y |
---|---|---|
1 | 0.3 | 10 |
2 | 5.15 | 0.5825 |
3 | 2.8662 | 1.0466 |
4 | 1.9564 | 1.5333 |
5 | 1.7449 | 1.7192 |
6 | 1.7320 | 1.7320 |
7 | 1.7320 | 1.7320 |
Java Implementation C# Implementation Python Implementation C++ Implementation
Babylonian Method Algorithm