How To Find Square Root In Python Without Math

Finding the square root of a number is a common operation in various mathematical problems, and Python is frequently used to perform these calculations.

However, you may want to find a square root without using the built-in math library in Python.

This tutorial will teach you how to find the square root without using the math library in Python, including an implementation of the Babylonian method for square root calculations.

In this tutorial, we will discuss two different approaches to finding the square root in Python without using the math library:

  1. Babylonian (Heron’s) method
  2. Binary search method

1. Babylonian (Heron’s) Method

The Babylonian method or Heron’s method is an ancient algorithm for finding the square root of a number. It works by iteratively improving the guess for the square root until an acceptable error is achieved. Here is how you can implement the Babylonian method:

Now, let’s find the square root using this function:

Output:

The square root of 36 is 6.0000000929222947

2. Binary Search Method

Another way to find the square root without using the math library is by using a binary search algorithm. This method starts by defining a range (0, number) and repeatedly updating the mid-point of this range as our guess for the square root, narrowing down the range until the desired precision is achieved.

Now, let’s find the square root using this function:

Output:

The square root of 36 is 6.00000011920929

Full Code

Conclusion

In this tutorial, we have learned how to find the square root of a number in Python without using the math library. We covered two different methods – the Babylonian method and the binary search method. Both methods provide accurate results, and you can choose the one that best suits your requirements.