Find Square Root Of Given Number - Python
Last Updated :
24 Feb, 2025
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))
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))
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.
Using Binary search
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))
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))
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
Python math.sqrt() function | Find Square Root in Python math.sqrt() returns the square root of a number. It is an inbuilt function in the Python programming language, provided by the math module. In this article, we will learn about how to find the square root using this function.Example:Pythonimport math # square root of 0 print(math.sqrt(0)) # square r
2 min read
numpy.roots() function - Python numpy.roots() function return the roots of a polynomial with coefficients given in p. The values in the rank-1 array p are coefficients of a polynomial. If the length of p is n+1 then the polynomial is described by: p[0] * x**n + p[1] * x**(n-1) + ... + p[n-1]*x + p[n] Syntax : numpy.roots(p) Parame
1 min read
Find Average of a Number Digits in Python AIn this article, we will see how to find the average of the digits of a number in Python. Examples: Input: N = 642 Output: 4.0 Explanation: (6+4+2)/3 = 12/3 = 4.0 Input: N = 3504 Output: 3.0 Explanation: (3+5+0+4)/4 = 12/4 = 3.0Calculate the average of a number of digits in python if the number is
3 min read
N-th root of a number Given two numbers n and m, find the n-th root of m. In mathematics, the n-th root of a number m is a real number that, when raised to the power of n, gives m. If no such real number exists, return -1.Examples: Input: n = 2, m = 9Output: 3Explanation: 32 = 9Input: n = 3, m = 9Output: -1Explanation: 3
9 min read
How to find the int value of a string in Python? In Python, we can represent an integer value in the form of string. Int value of a string can be obtained by using inbuilt function in python called as int(). Here we can pass string as argument to this function which returns int value of a string. int() Syntax : int(string, base) Parameters : strin
2 min read