Knowee
Questions
Features
Study Tools

Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well.You must not use any built-in exponent function or operator.For example, do not use pow(x, 0.5) in c++ or x ** 0.5 in python. Example 1:Input: x = 4Output: 2Explanation: The square root of 4 is 2, so we return 2.Example 2:Input: x = 8Output: 2Explanation: The square root of 8 is 2.82842..., and since we round it down to the nearest integer, 2 is returned.

Question

Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well.You must not use any built-in exponent function or operator.For example, do not use pow(x, 0.5) in c++ or x ** 0.5 in python. Example 1:Input: x = 4Output: 2Explanation: The square root of 4 is 2, so we return 2.Example 2:Input: x = 8Output: 2Explanation: The square root of 8 is 2.82842..., and since we round it down to the nearest integer, 2 is returned.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a Python solution using binary search:

def sqrt(x):
    if x < 2:
        return x

    left, right = 2, x // 2

    while left <= right:
        pivot = left + (right - left) // 2
        num = pivot * pivot
        if num > x:
            right = pivot -1
        elif num < x:
            left = pivot + 1
        else:
            return pivot

    return right

This function works by performing a binary search for the square root of x. It starts with a range of possible values for the square root, from 2 to x // 2. It then repeatedly calculates the square of the midpoint of this range, and adjusts the range based on whether this square is too high or too low. When the range has been narrowed down to a single value, this value is the square root of x, rounded down to the nearest integer.

This problem has been solved

Similar Questions

What does the following code print?import mathx=100while(math.sqrt(x)>0):    print("Square root larger than 0")x -= 5

Estimating the square root should be done in a static method declared as follows:12345678910/** * Computes estimate of square root of x to within relative error 0.01%. *  * @param x *            positive number to compute square root of * @return estimate of square root */private static double sqrt(double x) {    ...}

Krish, a curious explorer in a unique computational environment, explores numerical transformations. The system swiftly calculates and displays the cube for positive numbers, while it diligently calculates and returns the square value for negative numbers.Write a program to achieve this and use the return statement accordingly.Input format :The input consists of a single integer n.Output format :The output displays "Cube value: " followed by the cube value of n if the number is positive and terminates.The output displays "Squared value: " followed by the squared value of n if the number is negative or zero and terminates.Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases fall under the following constraints:-100 ≤ n ≤ 100Sample test cases :Input 1 :100Output 1 :Cube value: 1000000Input 2 :-20Output 2 :Squared value: 400Input 3 :0Output 3 :Squared value: 0

Change sqrt (including its Javadoc comments) so it also works when x = 0. Note: if your code from Newton1 appears to work without any changes, but it is such that it might execute a division by 0, then it is not correct. Division by 0, in general, is undefined and you should not write code that attempts to compute it.

x is a positive number. If it’s rational, plot its exact value on a number line. If it’s irrational, find an approximate location for x on a number line.Start by thinking of the value of x. What is one way to represent the value of x? Use square root notation to write your answer.x 2 = 72x =  √72Great job!Now, think about the value of 72​ . ​Is it equal to a whole number? In other words, is there any whole number you can square, or multiply by itself, to get a product of 72?x 2 = 72 x =  72​ Yes, 72​ is equal to a whole number.No, 72​ is not equal to a whole number.Great job!72​ is not equal to a whole number because there is no whole number that we can multiply by itself to find a product of 72. 72​ is irrational, so we can only find an approximation of its value. Which point below could represent the location on the number line for that approximate value?x 2 = 72x =  72​ 012345678910ABCDPoint APoint BPoint CPoint DGood work!Now that you’ve plotted x on the number line, consider another variable below, y. Which point could represent the approximate location of y on the number line? y = − 72​ -10-9-8-7-6-5-4-3-2-1012345678910

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.