Square root

by rishul matta

JavaScript

var sqr;
function sqrt(x, high, low) {

    start = 1, end = x;   
    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;
  
}
a = sqrt(25,25,1)
console.log(a);