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:

- Babylonian (Heron’s) method
- 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:

1 2 3 4 5 6 7 8 9 10 11 |
def babylonian_method(number, max_iterations=1000, error_margin=1e-6): if number < 0: return None guess = number for _ in range(max_iterations): guess = (guess + number / guess) / 2 error = abs(guess * guess - number) if error < error_margin: return guess return guess |

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

1 2 3 |
number = 36 square_root = babylonian_method(number) print("The square root of", number, "is", square_root) |

## 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.

1 2 3 4 5 6 7 8 9 10 11 12 |
def binary_search_method(number, error_margin=1e-6): if number < 0: return None left, right = 0, number while right - left > error_margin: mid = (left + right) / 2 if mid * mid > number: right = mid else: left = mid return (left + right) / 2 |

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

1 2 3 |
number = 36 square_root = binary_search_method(number) print("The square root of", number, "is", square_root) |

## Output:

The square root of 36 is 6.00000011920929

## Full Code

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
def babylonian_method(number, max_iterations=1000, error_margin=1e-6): if number < 0: return None guess = number for _ in range(max_iterations): guess = (guess + number / guess) / 2 error = abs(guess * guess - number) if error < error_margin: return guess return guess def binary_search_method(number, error_margin=1e-6): if number < 0: return None left, right = 0, number while right - left > error_margin: mid = (left + right) / 2 if mid * mid > number: right = mid else: left = mid return (left + right) / 2 number = 36 square_root_babylonian = babylonian_method(number) print("The square root of", number, "using Babylonian method is", square_root_babylonian) square_root_binary_search = binary_search_method(number) print("The square root of", number, "using Binary Search method is", square_root_binary_search) |

## 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.