Square root an integer using built-in functions

Below is the implementation for finding the square root using the built-in function. 

Python3




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


Output

3

Time Complexity: O(log(X))
Auxiliary Space: O(1)

Square Root Of Given Number using numpy.sqrt() 

To find the square root of a number using Numpy, you can use the numpy.sqrt() function. This function takes in a number or an array of numbers and returns the square root of each element.

Here is an example of how you can use numpy.sqrt() to find the square root of a number:

Python3




import numpy as np
 
# Find the square root of 9
sqrt_9 = np.sqrt(9)
print(sqrt_9)  # Output: 3.0
 
# Find the square root of a list of numbers
numbers = [4, 9, 16, 25]
sqrt_numbers = np.sqrt(numbers)
print(sqrt_numbers)  # Output: [2.0 3.0 4.0 5.0]


Output:

3.0
[2. 3. 4. 5.]

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

Time Complexity: The time complexity of the np.sqrt() function is O(1) for a single input and O(n) for an array of inputs, because it involves computing the square root of the input values. The time complexity is constant for a single input and linear in the size of the array for an array of inputs.

Space Complexity: The space complexity of the np.sqrt() function is O(1) for both a single input and an array of inputs, because it does not use any additional data structures and the space it uses is independent of the input size. The function returns a single value for a single input and an array of values for an array of inputs.

There can be many ways to solve this problem. For example, the Babylonian Method is one way. Please refer complete article on the Square root of an integer for more details!



Python Program To Find Square Root Of Given Number

Given an integer X, find its square root. If X is not a perfect square, then return floor(√x).

Examples:

Input: x = 4
Output: 2
Explanation: The square root of 4 is 2.

Input: x = 11
Output: 3
Explanation:  The square root of 11 lies in between 3 and 4 so floor of the square root is 3.

Similar Reads

Python Program to Find the Square Root

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

Square root an integer using Binary search

...

Square root an integer using built-in functions

The idea is to find the largest integer i whose square is less than or equal to the given number. The values of i * i is monotonically increasing, so the problem can be solved using binary search....