Open In App

Find Square Root Of Given Number – Python

Last Updated : 24 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an integer X, find its square root. If X is not a perfect square, then return floor(√x). For example, if X = 11, the output should be 3, as it is the largest integer less than or equal to the square root of 11.

Using built-in functions

We can also find the floor of the square root using Python’s built-in exponentiation operator (**) and then integer conversion.

Python
def countSquares(x):
    sqrt = x**0.5
    result = int(sqrt)  
    return result

# driver code
x = 9
print(countSquares(x))

Output
3

Explanation: x**0.5 calculates the square root of x, and int(sqrt) converts it to the largest integer less than or equal to the sqrt.

Using numpy + math library

The same result can be obtained by using the numpy.sqrt() function to compute the square root and then applying math.floor() to round it down to the nearest integer.

Python
import numpy as np
import math

sr = np.sqrt(10)

# Apply floor function
print(math.floor(sr))

Output
3

Explanation: np.sqrt(10) function computes the square root of 10, resulting in approximately 3.162. The math.floor(sr) function then rounds it down to 3

Note: numpy.sqrt() returns a Numpy array if the input is an array, and a single value if the input is a single number.

We can also use binary search to do the same task, making it more efficient than brute-force approaches. Instead of iterating through all numbers, it repeatedly halves the search space, significantly reducing the number of calculations.

Python
def floorSqrt(x):
    
    # Base case
    if (x == 0 or x == 1):
        return x

    # Do Binary Search for floor(sqrt(x))
    start = 1
    end = x//2
    while (start <= end):
        mid = (start + end) // 2

        # If x is a perfect square
        if (mid*mid == x):
            return mid

        # Since we need floor, we update answer when 
        # mid*mid is smaller than x, and move closer to sqrt(x)
        if (mid * mid < x):
            start = mid + 1
            ans = mid
        else:
            # If mid*mid is greater than x
            end = mid-1
    return ans

# driver code
x = 11
print(floorSqrt(x))

Output
3

Explanation: If x is 0 or 1, it returns x. Otherwise, it initializes start = 1 and end = x // 2 and performs binary search. It calculates mid and checks if mid² == x. If not, it adjusts start or end accordingly, storing the closest possible square root (ans).

Using Brute force

To find the floor of the square root, try with all-natural numbers starting from 1. Continue incrementing the number until the square of that number is greater than the given number.

Python
def floorSqrt(x):

    # Base cases
    if (x == 0 or x == 1):
        return x

    # Starting from 1, try all numbers until
    # i*i is greater than or equal to x.
    i = 1
    result = 1
    while (result <= x):
        i += 1
        result = i * i
    return i - 1

x = 18
print(floorSqrt(x))

Output
4

Explanation: Increment i while i * i is ≤ x. Once i * i exceeds x, it returns i – 1 as the largest integer whose square is ≤ x.



Similar Reads