# Babylonian method to find square root using Python

In this post, we are going to learn how to design a program to generate the square root of a number using the Babylonian method in Python. Though there are many methods to calculate the square root of a number, the Babylonian method is one of the commonly used algorithms and also one of the oldest methods in mathematics to calculate the square root of a number. This algorithm uses the idea of the Newton-Raphson method which is used for solving non-linear equations in mathematics. Let’s see how this algorithm works.

## The Babylonian methodÂ in Python

This method follows the trial and error strategy, which is characterized by repeated attempts until success occurs. First, we guess a number(the algorithm becomes more efficient if this number is close to the square root) x. We also initialize another variable( say y) to 1. Then we decide upon the maximum acceptable error. Then we run a recursive procedure until the difference between x and y is less than the maximum acceptable error. In the procedure, x is assigned with the arithmetic mean of x and y, and y is assigned with n/x.

See the code below to understand its working.

def babylonian_method(number): #x can be assigned with any number x = number #for simplicity we are assigning x with the number y = 1 error = 0.000000001 # maximum acceptable error while x - y > error: x = (x + y)/2; y = number/x; return x print("The square root of the number 9 is " + str(babylonian_method(9))) print("The square root of the number 14 is "+str(babylonian_method(14)))

**Output-**

The square root of the number 9 is 3.0 The square root of the number 14 is 3.7416573867739413

Thank you.

Also read: How to use the square root function sqrt() in Pytho

why doesn’t it work with 7 and tolerance of 10^-16?