Square root of an integer | Returns floor of square root of x ; Base cases ; Do Binary Search for floor ( sqrt ( x ) ) ; If x is a perfect square ; Since we need floor , we update answer when mid * mid is smaller than x , and move closer to sqrt ( x ) ; If mid * mid is greater than x ; Driver Code
< ? php function floorSqrt ( $ x ) { if ( $ x == 0 $ x == 1 ) return $ x ; $ start = 1 ; $ end = $ x ; $ ans ; while ( $ start <= $ end ) { $ mid = ( $ start + $ end ) / 2 ; if ( $ mid * $ mid == $ x ) return $ mid ; if ( $ mid * $ mid < $ x ) { $ start = $ mid + 1 ; $ ans = $ mid ; } else $ end = $ mid - 1 ; } return $ ans ; } $ x = 11 ; echo floorSqrt ( $ x ) , " STRNEWLINE " ; ? >