Python Example – Write a Python program to computing square roots using the Babylonian method.

(Python Example for Citizen Data Scientist & Business Analyst)

 

Write a Python program to computing square roots using the Babylonian method.

 

Perhaps the first algorithm used for approximating √S is known as the Babylonian method, named after the Babylonians, or “Hero’s method”, named after the first-century Greek mathematician Hero of Alexandria who gave the first explicit description of the method. It can be derived from (but predates by 16 centuries) Newton’s method. The basic idea is that if x is an overestimate to the square root of a non-negative real number S then S / x will be an underestimate and so the average of these two numbers may reasonably be expected to provide a better approximation.

 

Sample Solution:-

Python Code:


def BabylonianAlgorithm(number):
    if(number == 0):
        return 0;

    g = number/2.0;
    g2 = g + 1;
    while(g != g2):
        n = number/ g;
        g2 = g;
        g = (g + n)/2;

    return g;

print('The Square root of 0.3 =', BabylonianAlgorithm(0.3));

Sample Output:

The Square root of 0.3 = 0.5477225575051661

 

Write a Python program to computing square roots using the Babylonian method

Sign up to get end-to-end “Learn By Coding” example.



Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.