Find cubic root of a number | Returns the absolute value of n - mid * mid * mid ; Returns cube root of a no n ; Set start and end for binary search ; Set precision ; If error is less than e then mid is our answer so return mid ; If mid * mid * mid is greater than n set end = mid ; If mid * mid * mid is less than n set start = mid ; Driver Code
< ? php function diff ( $ n , $ mid ) { if ( $ n > ( $ mid * $ mid * $ mid ) ) return ( $ n - ( $ mid * $ mid * $ mid ) ) ; else return ( ( $ mid * $ mid * $ mid ) - $ n ) ; } function cubicRoot ( $ n ) { $ start = 0 ; $ end = $ n ; $ e = 0.0000001 ; while ( true ) { $ mid = ( ( $ start + $ end ) / 2 ) ; $ error = diff ( $ n , $ mid ) ; if ( $ error <= $ e ) return $ mid ; if ( ( $ mid * $ mid * $ mid ) > $ n ) $ end = $ mid ; else $ start = $ mid ; } } $ n = 3 ; echo ( " Cubic ▁ root ▁ of ▁ $ n ▁ is ▁ " ) ; echo ( cubicRoot ( $ n ) ) ; ? >