![]() |
Given an integer X, find its square root. If X is not a perfect square, then return floor(√x). Examples:
Python Program to Find the Square RootTo 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. Follow the steps below to implement the above idea
Below is the implementation of the above approach: Python3
Output
3 Complexity Analysis:
Square root an integer using Binary searchThe 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. Below is the implementation of the above idea:
Below is the implementation of the above approach: Python3
Output
3 Complexity Analysis:
Square root an integer using built-in functionsBelow is the implementation for finding the square root using the built-in function. Python3
Output
3 Time Complexity: O(log(X)) 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
Output: 3.0 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! |
Reffered: https://www.geeksforgeeks.org
Python Programs |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |